Android soundpool在播放大量声音时意外关闭应用程序,但没有显示任何消息
在我的应用程序中,我有一个makesound(int)方法,当我希望在游戏的不同阶段发出声音时,会调用该方法。 我已经创建了audiomanager和soundpool变量public,并在OnCreate方法中初始化了soundpool和audiomanagerAndroid soundpool在播放大量声音时意外关闭应用程序,但没有显示任何消息,android,soundpool,Android,Soundpool,在我的应用程序中,我有一个makesound(int)方法,当我希望在游戏的不同阶段发出声音时,会调用该方法。 我已经创建了audiomanager和soundpool变量public,并在OnCreate方法中初始化了soundpool和audiomanager audioManager = (AudioManager) this.getSystemService(this.AUDIO_SERVICE); soundPool = new SoundPool(5, AudioManager.ST
audioManager = (AudioManager) this.getSystemService(this.AUDIO_SERVICE);
soundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);
下面是我的makesound(int)方法
private void makeSound(int w){
if(onVolume) { //onvolume is a public boolean var that is set
final int volume = audioManager.getStreamVolume(AudioManager.STREAM_SYSTEM);
int id;
switch(w){
case 1:
id = soundPool.load(this, R.raw.metalimpact, 1);
break;
case 2:
id = soundPool.load(this, R.raw.swoosh, 1);
break;
case 3:
id = soundPool.load(this, R.raw.win, 1);
case 4:
id = soundPool.load(this, R.raw.win, 1);//second chance
break;
case 5:
id = soundPool.load(this, R.raw.fanfare, 1);//second chance
break;
default:id = soundPool.load(this, R.raw.metalimpact, 1);
}
final int soundID = id;
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
soundPool.play(soundID, volume, volume, 0, 0, 1f);
}
});
}
}
当我只有一个声音要播放时,它工作得很好。但是在我设置了switch case stmt并播放了很多声音之后,我注意到应用程序会在一段时间内正常运行,但突然崩溃,没有任何消息或任何东西。我的声音池加载太多了吗?。请告诉我我做错了什么。谢谢 您使用了错误的方法来实现您的逻辑 首先您不需要在makeSound(int w)方法中每次加载声音。而是先加载所有声音,然后管理它们,无论它们是否在LoadCompleteListener中加载
HashMap<Integer, Integer >loadedSound=new Map<>();
loadedSound.put(1,soundPool.load(this, R.raw.metalimpact, 1));
loadedSound.put(2,soundPool.load(this, R.raw.swoosh, 1));
loadedSound.put(3,soundPool.load(this, R.raw.win, 1));
loadedSound.put(4,soundPool.load(this, R.raw.win, 1));
loadedSound.put(5,soundPool.load(this, R.raw.fanfare,1));
switch(w)
{
case w: soundPool.play(loadedSound.get(w), volume, volume, 0, 0, 1f);
break;
default:soundPool.play(loadedSound.get(1), volume, volume, 0, 0, 1f);
}