开发者

How to prevent SoundPool from truncating sounds

I have a soundboard app and I use SoundPool to control the playback of my sounds. Everything worked great until I tried to add a sound that was about 30 seconds long. When I click the button for that sound it only plays for a few seconds then stops. Is there a way to prevent this from happening using SoundPool? Here is my code. Thanks!

public class SoundManager {

private  SoundPool mSoundPool; 
private  HashMap<Integer, Integer> mSoundPoolMap; 
private  AudioManager  mAudioManager;
private  Context mContext;


public SoundManager()
{

}

public void initSounds(Context theContext) { 
     mContext = theContext;
     mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0); 
     mSoundPoolMap =开发者_如何转开发 new HashMap<Integer, Integer>(); 
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);         
} 

public void addSound(int Index,int SoundID)
{
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));

}

public void playSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}

public void playLoopedSound(int index)
{
    float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}


There are some Soundpool information I pick up while developing my music app. Here are some guide lines that not covered in the original documentation base on limitations with length of sound:

   Length of Sound              Khz
--------------------      ---------------
     1-5 Secs.             44khz - Stereo
    6-11 Secs.             22khz - Stereo
   12-21 Secs.             11khz - Mono
     21+ Secs.              8khz - Mono 

So if you have any audio files that are getting cut off by SoundPool, convert using this. Of course when converting to 11khz or below, the audio start to sound a bit low quality, but better than nothing. The program I use to convert is: Free MP3 WMA OGG Converter. I always use ogg files when dealing with Soundpool. Hope this help folks.


SoundPool has an internal memory limitation of 1 (one) Mb. You might be hitting this if your sound is very high quality. If you have many sounds and are hitting this limit, just create more soundpools, and distribute your sounds across them.


SoundPool is a lightweight player which will only decode the first 1Mb data, but if you want to use a more high quality or long audio file, you need to:

* Use MediaPlayer interface instead of SoundPool. MediaPlayer is more complicated mechanism so the playback startup time longer than SoundPool.
* Use MediaCodec API to decode data firstly then call AudioTrack API to playback PCM data as requested.This solution will decode the data firstly then do PCM playback directly, so the delay will be the same as SoundPool but it's more complicated for programming.


it is only meant for playing short sounds, read the SDK page.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜