我在Android中编写一个棋盘游戏,每次点击都需要一个声音效果。我正在使用声音池。会有很多点击,所以我想知道我必须在哪里声明SoundPool并加载它?在onCreate函数中,还是有一个创建,加载和播放声音的函数,每次点击都会调用它?
//make soundpool public
SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//load sound here and play wherever needed
int soundID=soundPool.load(this,R.raw.metalimpact,1);
}
//or put it all in a function
private void makeSound(){
SoundPool soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
final int soundID=soundPool.load(this,R.raw.metalimpact,1);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener(){
public void onLoadComplete(SoundPool soundPool, int sampleId,
int status) {
soundPool.play(soundID, 1, 1, 0, 0, 1f);
}});
}
什么是强制SoundPool的正确方法?
我实施它的方式会出现性能问题吗?
我应该何时释放soundpool对象?..在OnDestroy或onPause()中?
SoundPool也已被弃用,但支持的最小api级别是11.那我该怎么办? 请帮忙!!