Is there a reliable way to prevent truncating of sounds in soundpool? I have had some success with the sleep() function between sounds, but they still sometimes miss the last bit of sound before starting another sound. My app plays short sounds in sequence. Jerry
Although this is an old question, I thought I would post something here since I couldn't find a solution to this problem online but did come up with something of a solution...
I get the duration of each sound (using MediaPlayer) when my app starts up, and store the R.id.sound_name value and the duration together. I can then play the sounds using the SoundPool.
Code to get duration:
private long getSoundDuration(int rawId){
MediaPlayer player = MediaPlayer.create(context, rawId);
int duration = player.getDuration();
return duration;
}
In my sound class, I then play the sound as usual then sleep for the duration. Pretty simple stuff.
Seems to work well. Gives me the advantages of low-latency from SoundPool, plus ability to chain things.
A couple things to note:
- Don't get the duration each time, as creating a MediaPlayer has overhead.
- Durations are taken from file metadata -- ogg's work great for me, but I can't vouch for anything else.
Make sure you have enough Streams. in the SoundPool constructor, the first argument is MaxStreams. Set that to whatever you seem fit. 8 or so should be enough i guess... but you might need more, depending on your sounds.
来源:https://stackoverflow.com/questions/3663794/android-soundpool-timing