[英]How to properly release SoundPool objects?
以下代碼播放隨機聲音。 我正試圖釋放它,因為我不再需要按鈕來播放任何其他內容,但是當我運行應用程序時,該按鈕根本不會播放任何聲音。
public class actibida extends AppCompatActivity {
SoundPool soundPool;
Button button;
boolean loaded = false;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_actibida);
soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
@Override
public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
loaded = true;
}
});
//I have a total of four sounds
final int[] sound = new int[4];
sound[0] = soundPool.load(actibida.this, R.raw.el, 1);
sound[1] = soundPool.load(actibida.this, R.raw.guau, 1);
sound[2] = soundPool.load(actibida.this, R.raw.miau, 1);
sound[3] = soundPool.load(actibida.this, R.raw.quack, 1);
final Random r = new Random();
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View button) {
final Random r = new Random();
int Low = 0;
int High = 3;
int rndm = r.nextInt(High - Low) + Low;
soundPool.play(sound[r.nextInt(4)], 1.0f, 1.0f, 0, 0, 1.0f);
}
});
soundPool.release();
}
}
有人可以教我如何正確調用.release()嗎?
PS如果我要在應用程序中播放其他聲音,我應該使用不同的SoundPool類嗎? 或者我應該釋放它並再次加載它? 事情是我甚至不能在第一時間釋放,所以我不知道。
您將在聲音播放完畢之前釋放SoundPool。 事實上,你在播放開始的那一刻就釋放了它。 play()方法不等待聲音完成 - 播放是異步的。 此外,沒有辦法找出播放何時結束。
如果您將來要使用相同的聲音池,您可以簡單地將SoundPool設置為某種單例或將其存儲在可以重復使用的某個地方。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.