好的,我得到了这个FC错误。
我已经检查了代码三次,并尝试了两次重写。这是我能学到的最有效的代码。但我还是得到了FC。我将感谢任何帮助,因为我正试图为我的程序播放大约50个声音文件。每项活动在15到20之间。
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();
}}
发布于 2011-02-02 01:28:02
您创建的MediaPlayer对象太多,这很容易导致异常。您还将在onDestroy()方法中释放它们,该方法在许多情况下不会被调用。
更改您的代码以使用SoundPool。
发布于 2011-02-02 02:44:30
哇!为什么要使用这么多MediaPlayer对象?为什么不只有一个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的所有语句包围起来,并通过将某些内容输出到LogCat的copius语句来简化调试。您可以获得有关将调试信息打印到LogCat here的更多信息,以及有关哪条语句引发哪条异常here的MediaPlayer文档。
HTH,
斯里拉姆
发布于 2011-02-05 07:53:46
这个问题已经得到了回答,只是根据Sriram的建议进行了错误检查。
为soundpool错误启动新线程以使其更清晰。
https://stackoverflow.com/questions/4864586
复制相似问题