我已经尽了一切努力来完成这项工作。 Android 的 SoundPool 类正在加载 .ogg 音频文件(已测试),但无法播放。 由于某种奇怪的原因,我无法使其正常工作。我已经关注了许多关于此事的教程和论坛,但仍然无法使其正常工作。
这是我的代码:
作为全局变量:
SoundPool soundPool;
boolean ya = false;
在我的 Activity 的 onCreate 上:
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
soundPool = new SoundPool.Builder()
.setMaxStreams(15)
.build();
}else{
soundPool = new SoundPool(15, AudioManager.STREAM_MUSIC, 0);
}
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool sp, int sampleId, int status) {
ya = true;
}
});
soundPool.load(this, R.raw.explosion_1, 1);
最后,关于我的一个按钮的 onClick 方法:
public void instructions(View v){
if(ya){
soundPool.play(R.raw.explosion_1, 0.99f, 0.99f, 1, 0, 0.99f);
}
}
请帮忙。
最佳答案
为了播放声音,您需要将从 load()
获取的 id
传递到 play()
中。
int explosion = soundPool.load(this, R.raw.explosion_1, 1);
soundPool.play(explosion, 0.99f, 0.99f, 1, 0, 0.99f);
关于Android的SoundPool不播放任何声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30090967/