Although this is an old question, it seemed to me that I would post something here, since I could not find a solution to this problem on the Internet, but came up with something like a solution ...
I get the duration of each sound (using MediaPlayer) when the application starts and save the value of R.id.sound_name and duration together. Then I can play sounds using SoundPool.
Code for getting duration:
private long getSoundDuration(int rawId){
MediaPlayer player = MediaPlayer.create(context, rawId);
int duration = player.getDuration();
return duration;
}
In my sound class, I play the sound as usual, and then sleep for a while. Pretty simple stuff.
Seems to work well. Gives me the benefits of low latency from SoundPool, plus the ability to cling to things.
A few notes:
- Do not get a duration every time since creating MediaPlayer has overhead.
- Durations come from file metadata - ogg works great for me, but I can't vouch for anything else.
source
share