好的,我收到了这个FC错误。
我已经完成了三次代码并尝试了两次重写。这是我能学到的最有效的代码。但我仍然得到FC。 我希望得到任何帮助,因为我想为我的程序播放大约50个声音文件。每次活动十五到二十。
implements OnClickListener {
MediaPlayer mp1;
MediaPlayer mp2;
MediaPlayer mp3;
MediaPlayer mp4;
MediaPlayer mp5;
MediaPlayer mp6;
MediaPlayer mp7;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.verbs);
mp1 = MediaPlayer.create(this, R.raw.play);
mp2 = MediaPlayer.create(this, R.raw.eat);
mp3 = MediaPlayer.create(this, R.raw.can);
mp4 = MediaPlayer.create(this, R.raw.go);
mp5 = MediaPlayer.create(this, R.raw.help);
mp6 = MediaPlayer.create(this, R.raw.practice);
mp7 = MediaPlayer.create(this, R.raw.use);
final Button button1 = (Button) findViewById(R.id.play_button);
button1.setOnClickListener(this);
final Button button2 = (Button) findViewById(R.id.eat_button);
button2.setOnClickListener(this);
final Button button3 = (Button) findViewById(R.id.can_button);
button3.setOnClickListener(this);
final Button button4 = (Button) findViewById(R.id.go_button);
button4.setOnClickListener(this);
final Button button5 = (Button) findViewById(R.id.Thelp_button);
button5.setOnClickListener(this);
final Button button6 = (Button) findViewById(R.id.pract_button);
button6.setOnClickListener(this);
final Button button7 = (Button) findViewById(R.id.use_button);
button7.setOnClickListener(this);
final Button button8 = (Button) findViewById(R.id.Back_Button);
button8.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.play_button:
mp1.start();
Toast.makeText(VerbsActivity.this, "PLAY",
Toast.LENGTH_LONG).show();
break;
case R.id.eat_button:
mp2.start();
Toast.makeText(VerbsActivity.this, "EAT",
Toast.LENGTH_LONG).show();
break;
case R.id.can_button:
mp3.start();
Toast.makeText(VerbsActivity.this, "CAN",
Toast.LENGTH_LONG).show();
break;
case R.id.go_button:
mp4.start();
Toast.makeText(VerbsActivity.this,"GO",
Toast.LENGTH_LONG).show();
break;
case R.id.Thelp_button:
mp5.start();
Toast.makeText(VerbsActivity.this,"HELP",
Toast.LENGTH_LONG).show();
break;
case R.id.pract_button:
mp6.start();
Toast.makeText(VerbsActivity.this, "PRACTICE",
Toast.LENGTH_LONG).show();
break;
case R.id.use_button:
mp7.start();
Toast.makeText(VerbsActivity.this, "USE",
Toast.LENGTH_LONG).show();
break;
case R.id.Back_Button:
finish();
break;
}
}
@Override
protected void onDestroy() {
mp1.release();
mp2.release();
mp3.release();
mp4.release();
mp5.release();
mp6.release();
mp7.release();
super.onDestroy();
}
}
答案 0 :(得分:0)
您正在创建太多的MediaPlayer对象,这很容易导致异常。您也在onDestroy()
方法中释放它们,在许多情况下不会调用它。
更改您的代码以改为使用SoundPool。
答案 1 :(得分:0)
哇!你为什么要使用这么多的MediaPlayer对象?为什么不单个媒体播放器对象,然后在单击该按钮时为其分配资源?我不知道你的应用程序做了什么,但我会编写上面的代码(或至少它的片段),如下所示:
MediaPlayer mp1; //just have one MediaPlayer object.
public void onClick(View v) {
switch(v.getId()) {
case R.id.n1_button:
if(mp1.create(this, R.raw.sound1) == NULL) {
Log.v(this.toString(), "Unable to create mediaplayer object.");
}
try {
mp1.start();
} catch(IllegalStateException e) {
e.printStackTrace();
Log.v(this.toString(), "Illegal State Exception caught in start.");
}
Toast.makeText(NounsActivity.this, "word",
Toast.LENGTH_LONG).show();
break;
case R.id.n2_button:
if(mp1.create(this, R.raw.sound1) == NULL) {
Log.v(this.toString(), "Unable to create mediaplayer object.");
}
try {
mp1.start();
} catch(IllegalStateException e) {
e.printStackTrace();
Log.v(this.toString(), "Illegal State Exception caught in start.");
}
Toast.makeText(NounsActivity.this, "word",
Toast.LENGTH_LONG).show();
break; and so on....
@Override
protected void onDestroy() {
mp1.release(); //you just need to release one mediaplayer object now.
super.onDestroy();
}
但更一般的说明:围绕在try
和catch
语句块中调用MediaPlayer的所有语句,并通过copius语句轻松调试,以便向LogCat打印内容。您可以获取有关将调试信息打印到LogCat here和MediaPlayer文档的更多信息,以了解哪个语句会抛出哪个异常here。
HTH,
斯利拉姆。
答案 2 :(得分:0)
回答问题只是对Sriram的建议进行错误检查。
为soundpool错误启动新线程以使其更清晰。