Ok I am getting this FC error.
I have gone over the code three times and have tried two re-writes. Here is the most efficient code that I was able to learn. But I am still getting FC. I would appreciate any help as I am trying to get to play around 50 sound files for my program. between fifteen to twenty per activity.
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()开发者_JAVA技巧) {
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();
}
}
You are creating too many MediaPlayer objects, which can easily cause an exception. You are also releasing them in the onDestroy()
method, which won't be called in many cases.
Change your code to use a SoundPool instead.
Wow! Why are you using so many MediaPlayer objects? Why not have a single mediaplayer object and then allocate resources to it when that button is clicked? I don't know what your application does, but I would have written the above code (or atleast a snippet of it) like so:
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();
}
But on a more general note: Surround all those statements where the MediaPlayer is invoked within try
and catch
statement blocks and give yourself the benefit of easy debugging through copius statements that print something to LogCat. You can get more information on printing debug information to LogCat here and MediaPlayer documentation regarding which statement throws which exception here.
HTH,
Sriram.
The Question has been answered just doing Error checks on Sriram's advice.
Starting new thread for the the soundpool error to make it more clear.
精彩评论