我使用相同的确切代码,但使用.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;
}
我知道这很多 - 但那些是静态方法调用,所以它应该没问题,对吧?为什么会崩溃?
答案 0 :(得分:3)
您一个接一个地创建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
。