I have a problem. When I try to play sound using Soundpool there is small pause between each playing.
For example, I have 2 sec-sound which I want to play when button is pressed.
I tried to start 2 threads with the same sound. Second started 0.5 sec after first. But there is no helpful.
AudioManager mAudioManager;
SoundPool mSoundPool;
Runnable thr1;
Runnable thr2;
public void playLoopedSound(int soundId) {
mSoundPool = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)*2;
thr1 = new Runnable() {
public void run() {
mSoundPool.play(soundId, 0, streamVolume, 1, -1, 1f); //0 = no loop, -1 = loop forever
}
};
thr2 = new Runnable() {
public void run() {
mSoundPool.play(soundId, 0, streamVolume, 1, -1, 1f);
}
};
thr1.run();
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
thr2.run();
}
It’s hard to know without debugging, but I’ve had similar issues due to the performance of soundpool when it’s loading sounds. To get around this, I implemented SoundPool.onLoadCompleteListener to ensure that I didn’t start trying to play sounds until the soundpool was fully loaded.
Be aware that this is only available on API8 or above. See this answer for a nice implementation:
Knowing if the loading of a sound with SoundPool has been successful on Android 1.6/2.0/2.1