最近我一直在开发一款android游戏,但在添加音乐和其他声音时遇到了问题。我读到Mediaplayers最适合播放背景音乐,效果很好,我还读到Soundpool最适合播放短小的音乐片段,比如音效。但是,我无法让Soundpool工作,似乎Soundpool甚至没有加载。
我做了更多的研究,发现了很多关于Soundpool缺乏支持的噩梦故事,所以我应该只使用mediaplayer来处理所有的音频吗?这是有效的吗?
//My Code
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int mySoundId, int status) {
loaded = true;
}
});
if(loaded==true){
soundId = soundPool.load(this, R.raw.sound1, 1);
soundPool.play(soundId, 1, 1, 1, 0, 1f);}
else{
System.exit(0);
}
//Forces the program to exit everytime发布于 2014-03-24 19:54:21
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int mySoundId, int status) {
loaded = true;
soundId = soundPool.load(this, R.raw.sound1, 1);
soundPool.play(soundId, 1, 1, 1, 0, 1f);
}
});因为您加载的变量将始终为false,所以您应该在onLoadComplete中执行这些操作。
发布于 2020-03-24 14:19:46
SoundPool#load方法是异步的,应在调用onLoadComplete后播放声音
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int mySoundId, int status) {
soundPool.play(mySoundId, 1, 1, 1, 0, 1f);
}
});
soundPool.load(this, R.raw.sound1, 1);https://stackoverflow.com/questions/22572879
复制相似问题