24

I use Soundpool in my app, so far it works good, but I do have a wav file which is 10 secs. Unfortunately, soundpool plays only the first 5 secs. How to make soundpool to play the whole track? I have converted wav to -- ogg and mp3 still the same issue. It plays only the first 5 secs. Any help would be much appreciated.

//set up audio player
mSoundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
//load fx
mSoundPoolMap.put(RAW_1_1, mSoundPool.load(this, R.raw.loop1, 1));
//playing soundpool
case R.id.button1:          
mSoundPool.stop(mStream1);
mStream1= mSoundPool.play(mSoundPoolMap.get(RAW_1_1), streamVolume, streamVolume, 1, LOOP_1_TIME, 1f);

UPD Last: Maybe someone will find it here and read it. Seems soundpool cant play more then 5 secs. It is his maximum, for more longer sounds use MediaPlayer. I hope you will not spend so much of your time like i did)

Daler
  • 1,205
  • 3
  • 18
  • 39
  • @Yahor10, I have added a piece of code, do you want to have that wav file also? Because i have tried with other wavs, the same result not more then 5 secs – Daler Nov 14 '12 at 11:33
  • Anything in your logcat? I know soundpool files are restricted to being rather short (1MB uncompressed PCM data), maybe it's too long? There should be some message in the logcat I think. – Tim Nov 14 '12 at 15:41
  • @Tim logcat is clear, nothing here, and all my files are not more then 1 mb – Daler Nov 14 '12 at 16:03
  • The same problem, but my limit 10 seconds for any file - 18, 21 sec long, whatever. Make me crazy – Henadzi Rabkin Mar 12 '14 at 11:24
  • thanks for updating but I understand apps can crash if using more than 1 mediaplayer can you confirm if you succesfully used MediaPlayer with more than 1 sound? – CommonSenseCode Jun 11 '15 at 22:16
  • 1
    @cooervo, I had kinda music app with 4 MediaPlayers button. So all those 4 buttons could be activated at the same time, and I had no issue with running 4 Mediaplayers looping at the same time. So, it will not crash your app. Confirmed. It was waay long time ago, I believe nowadays its much more improved. – Daler Jun 12 '15 at 04:27

5 Answers5

15

So I think you reached the 1M limit in SoundPool.

SoundPool is hard code the buffer size as 1M, for all loaded file, store in pcm format.

So it do not care of ogg or wav.

ifreedom
  • 426
  • 3
  • 4
  • 1
    em, for audio, there's also a hard limit of 32 active AudioTrack object (SoundPool, MediaPlayer use AudioTrack for playing) per _device_ (not per app). – ifreedom Nov 23 '12 at 15:48
  • how can i reach 1M limit? 2shared.com/audio/aYty0aVm/loop1.html(click on second download row) --download this loop and check it on your side with soundpool, if you can succeed. Add only this loop and it is less then 1M. I think soundpool has been designed to play short sounds, and 5 secs it's his top limit. – Daler Nov 25 '12 at 04:58
  • Is this documented *anywhere*? I wouldn't have just wasted two hours of my life if the [SoundPool page](http://developer.android.com/reference/android/media/SoundPool.html) had mentioned that my sounds might be too long. – Matt Gibson May 03 '14 at 08:19
  • my audio is an ogg file with 113Kb. His duration is about 7sec and it's being cutted of on 5sec approximately. So I think there is also a duration limit. Not sure – Sérgio S. Filho Apr 24 '16 at 20:03
  • 1
    @SérgioS.Filho ogg format will decompress to pcm format, the actual size is relative to its Length and Sample Rate. – ifreedom Jan 04 '17 at 03:43
  • ifreedom is right. I saved an about 24 second mp3 source as a 22kHz (even though 22kHz is not listed on https://developer.android.com/guide/topics/media/media-formats) or 44 kHz 16 bit PCM wave file which produces a 1.0 MiB or 2.0 MiB file respectively. In android the sound in the smaller fades to inaudibility at 23 seconds matching the original in my pc's audio editor. In android the larger file truncates at about 12 seconds, the time where a 44 kHz 16 bit audio stream hits 1 MiB in size. Whether the limit is 1MiB or 1 MB, the time difference at these rates amounts to less than half a second. – Smartybartfast Jun 13 '18 at 05:49
12

We have solved similar problem by decreasing in our *.ogg effects sample rate. Our initial sample rate was 44 kHz ~ and only 10 sec of sound played, decreasing to 16 kHz increase playability to 30 seconds

Solution was found in this discussion

Henadzi Rabkin
  • 6,834
  • 3
  • 32
  • 39
  • 1
    +1 about effects of sample rate to sound play duration. However, we can't decrease sample rates all time, so in any case, for longer sounds would be a good decision to use MediaPlayer. Both have their advantages, so the decision should be make by dev. Thanks again for posting your solution, it's interesting. – Daler Mar 13 '14 at 14:01
11

SoundPool designed to play short sound effects. To play music (big audio files) you need to use MediaPlayer

// R.raw.audio_file - res/raw/audio_file.mp3

mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();
Vadim Zin4uk
  • 1,716
  • 22
  • 18
3

This activity is works for me

public class MainActivity extends Activity {

    private Integer soundID;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        // Load the sound
        AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
        float actualVolume = (float) audioManager
            .getStreamVolume(AudioManager.STREAM_MUSIC);
        float maxVolume = (float) audioManager
            .getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        final float volume = actualVolume / maxVolume;

        SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);

        soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
          @Override
          public void onLoadComplete(SoundPool soundPool, int sampleId,
              int status) {
                      @Override
      public void onLoadComplete(SoundPool soundPool, int sampleId,
          int status) {
          soundPool.play(soundID, volume, volume, 1, 0, 1f);
          try {
            Thread.sleep(5000); // play twice
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          soundPool.play(soundID, volume, volume, 1, 0, 1f);
      }
    });
          }
        });
        soundID = soundPool.load(this, R.raw.sound2, 1);
      //load fx
      //playing soundpool

    }
}

You must load you sound asynchrony and check audio cache - it can be overflow.

Read this article http://www.google.by/url?http://www.vogella.com/articles/AndroidMedia/article.html

Its very heedful

Yahor10
  • 2,123
  • 1
  • 13
  • 13
  • in your code nothing different, mine is the same. Im creating app for minsdk-7 onLoad required api-8. To point, if the sound will not be loaded, it will not play at all. Why is i need to check if its loaded or not? In my case it plays but only the firs 5 secs. Seems it is soundpool issue. Try by yourself if you can play a file which is more then 5 secs, try to play 10 sec – Daler Nov 14 '12 at 12:31
  • here is a link http://www.2shared.com/audio/aYty0aVm/loop1.html, click on second "download"- raw.. tahnks for your help – Daler Nov 14 '12 at 13:15
  • Thread.sleep(5000); // play twice --- I cant have it in my code at all, on the time when this loop plays, there are other sounds like snare, cymbal and etc. which has to be played by clicking on other buttons.. so – Daler Nov 14 '12 at 14:07
0

Your file is probably too large. Try to change from stereo to mono, if you do not need stereo anyway. Or try to downsample your file.

Reducing the filesice worked in my case.

Anne
  • 61
  • 2