Raghav Sood..
27
该的Soundpool是这个正确的类.以下代码是如何使用它的示例.它也是我在我的几个应用程序中用来管理声音的代码.您可以根据自己的喜好(或内存允许)发出声音.
public class SoundPoolPlayer {
private SoundPool mShortPlayer= null;
private HashMap mSounds = new HashMap();
public SoundPoolPlayer(Context pContext)
{
// setup Soundpool
this.mShortPlayer = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSounds.put(R.raw., this.mShortPlayer.load(pContext, R.raw., 1));
mSounds.put(R.raw., this.mShortPlayer.load(pContext, R.raw., 1));
}
public void playShortResource(int piResource) {
int iSoundId = (Integer) mSounds.get(piResource);
this.mShortPlayer.play(iSoundId, 0.99f, 0.99f, 0, 0, 1);
}
// Cleanup
public void release() {
// Cleanup
this.mShortPlayer.release();
this.mShortPlayer = null;
}
}
你可以通过调用:
SoundPoolPlayer sound = new SoundPoolPlayer(this);
在你的Activity的onCreate()中(或之后的任何时间).之后,播放声音简单的通话:
sound.playShortResource(R.raw.);
最后,一旦你完成了声音,请致电:
sound.release();
释放资源.