gpt4 book ai didi

java - 为什么播放mp3会强制崩溃?

转载 作者:行者123 更新时间:2023-12-02 00:39:31 24 4
gpt4 key购买 nike

我使用了完全相同的代码,但使用的是 .wav 文件,它工作了大约 50 次,然后崩溃了。我切换到 mp3 文件,它们根本无法播放。

程序本身工作正常 - 我知道在下面的情况下会发生错误:

case R.id.playButton: 
switch (korean.getIndex()){
case -1: break; // This is the case when the program first starts.
case 0: MediaPlayer.create(this, R.raw.c0).start(); break;
case 1: MediaPlayer.create(this, R.raw.c1).start(); break;
case 2: MediaPlayer.create(this, R.raw.c2).start(); break;
case 3: MediaPlayer.create(this, R.raw.c3).start(); break;
case 4: MediaPlayer.create(this, R.raw.c4).start(); break;
case 5: MediaPlayer.create(this, R.raw.c5).start(); break;
case 6: MediaPlayer.create(this, R.raw.c6).start(); break;
case 7: MediaPlayer.create(this, R.raw.c7).start(); break;
case 8: MediaPlayer.create(this, R.raw.c8).start(); break;
case 9: MediaPlayer.create(this, R.raw.c9).start(); break;
case 10: MediaPlayer.create(this, R.raw.c10).start(); break;
case 11: MediaPlayer.create(this, R.raw.c11).start(); break;
case 12: MediaPlayer.create(this, R.raw.c12).start(); break;
case 13: MediaPlayer.create(this, R.raw.c13).start(); break;
case 14: MediaPlayer.create(this, R.raw.v14).start(); break;
case 15: MediaPlayer.create(this, R.raw.v15).start(); break;
case 16: MediaPlayer.create(this, R.raw.v16).start(); break;
case 17: MediaPlayer.create(this, R.raw.v17).start(); break;
case 18: MediaPlayer.create(this, R.raw.v18).start(); break;
case 19: MediaPlayer.create(this, R.raw.v19).start(); break;
case 20: MediaPlayer.create(this, R.raw.v20).start(); break;
case 21: MediaPlayer.create(this, R.raw.v21).start(); break;
case 22: MediaPlayer.create(this, R.raw.v22).start(); break;
case 23: MediaPlayer.create(this, R.raw.v23).start(); break;
default: break;
}

我知道这很多——但这些是静态方法调用,所以应该没问题,对吧?为什么会崩溃?

最佳答案

您一个接一个地创建 MediaPlayer 对象并且从不释放资源,因此您可能会耗尽内存,请考虑:

// declare mp as class field.
MediaPlayer mp = null;

//...

case R.id.playButton:
if (mp != null)
mp.release();
switch (korean.getIndex()){
case -1: break; // This is the case when the program first starts.
case 0: mp = MediaPlayer.create(this, R.raw.c0);mp.start(); break;
case 1: mp = MediaPlayer.create(this, R.raw.c1);mp.start(); break;
case 2: mp = MediaPlayer.create(this, R.raw.c2);mp.start(); break;
// and so on

或者,您可以set OnCompletionListener并在那里调用release

关于java - 为什么播放mp3会强制崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6766004/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com