Coo*_*ter 13 android wav button media-player
我想在按下按钮后播放声音.MediaPlayer工作正常,但我在某处读到这个库很长.wav(就像音乐一样).
有没有更好的方法来打短.wav(2-3秒)?
Rag*_*ood 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.<sound_1_name>, this.mShortPlayer.load(pContext, R.raw.<sound_1_name>, 1));
mSounds.put(R.raw.<sound_2_name>, this.mShortPlayer.load(pContext, R.raw.<sound_2_name>, 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;
}
}
Run Code Online (Sandbox Code Playgroud)
你可以通过调用:
SoundPoolPlayer sound = new SoundPoolPlayer(this);
Run Code Online (Sandbox Code Playgroud)
在你的Activity的onCreate()中(或之后的任何时间).之后,播放声音简单的通话:
sound.playShortResource(R.raw.<sound_name>);
Run Code Online (Sandbox Code Playgroud)
最后,一旦你完成了声音,请致电:
sound.release();
Run Code Online (Sandbox Code Playgroud)
释放资源.
| 归档时间: |
|
| 查看次数: |
15032 次 |
| 最近记录: |