0

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()) {
    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();
}

}

BluMouse
  • 5
  • 1
  • 6
  • 5
    A stack trace for the FC would be useful, but its already very clear you should be using a SoundPool rather than 15 seperate MediaPlayers. – Reuben Scratton Feb 01 '11 at 15:41
  • 1
    Also describing the circumstances that cause it to crash... does it die on startup, does it run for a few mins then crash, will it play a certain number of sounds before failing? – dave.c Feb 01 '11 at 15:47

3 Answers3

0

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.

Ian G. Clifton
  • 9,349
  • 2
  • 33
  • 34
  • – Reuben Scratton Ian G. Clifton I saw something about soundpool on another post but when I started googling it the posts I found were that it was not finished and people could not get it to work. As I am a beginner I thought since it was not ready then I most certainly could not get it to run. As for the FC type the program loads up but when I want to go into the activity that has the Media players it FCs back to the previous activity. I have tried the above where I only use one media player but it still FCs back to the previous activity. – BluMouse Feb 02 '11 at 08:30
  • I'd verify the age of the posts saying it isn't finished as they may simply be old. I've used SoundPool on Android 1.5-2.3 without an issue, though only playing one sound at a time. The problem with multiple MediaPlayers is that the system has limited resources and might not be able to create ~15 of them even in the best circumstances. If just creating one is causing failure, then something else is going on. Try editing your post to include a stack trace and we should be able to help more. – Ian G. Clifton Feb 02 '11 at 20:54
  • I read up on this soundpool thing. I also looked at a couple of tutorials and this thing I just have to say thanks to you all because this fixed my problem right quick. Though I must say that I had to turn my sound files into ogg files and that fixed the simple black screen that was poping up. thank you all. – BluMouse Feb 03 '11 at 11:07
  • @Ian G. Clifton Ok with soundpool I ran into this error. well not an error but well let me explain. I tried both examples from this link. http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html and http://www.droidnova.com/creating-sound-effects-in-android-part-2,695.html the first one only worked if I had one sound file. when I tried to do more than the one it would fc. the second worked great. Which is why I was happy.But I have three activities that will use fifteen sounds each. and well with one activity it worked. with all three only the third one worked – BluMouse Feb 04 '11 at 13:54
  • @Ian G. Clifton But only the third would work and only one of the sound files would work on the third one. so if I did a rough image of how my soundpool was setup it was like this. from the tutorials I used their soundmanager file and added it like the document stated. In src file> soundmanager.java main.java sounds.java soundactivity1 soundactivity2 soundactivity3. when I use the program I can get to sounds from main but having 45 sounds in the soundmanager fifteen for each activity 1 and 2 FC. 3 opens but only plays the last sound on the first button. all other sound buttons make FC. – BluMouse Feb 04 '11 at 13:55
  • It's tough to say for sure without seeing your code. Perhaps you could mark this one as answered and post a new question with a sample of how you're using SoundPool? – Ian G. Clifton Feb 04 '11 at 22:46
  • @Ian G. Clifton http://stackoverflow.com/questions/4904098/soundpool-crashes-in-two-scenerios-code-for-first-scenerio-crashes – BluMouse Feb 06 '11 at 01:45
0

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.

Sriram
  • 10,298
  • 21
  • 83
  • 136
  • I tried this where it would only use one media player. But still before the activity is launched it FCs. So the program loads and when I click the button to change to that activity it FCs. But the wierd thing is that it does not crash all the way out it just FCs back on activity. Sorry I am a very fresh beginner so I do not know how to set a catch statement. or a try one as well. – BluMouse Feb 02 '11 at 08:28
  • I did some catch thanks Sriram though I don't know what the answer means.ShellCommandUnResponsive and then other stuff. Unless it is the wrong one. But still didnt work. – BluMouse Feb 02 '11 at 09:00
  • @BluMouse: I have edited the code above to give you a sample try and catch statement block with printing to LogCat. You can see the statements printed to LogCat in Eclipse by opening its window. In Eclipse this is done by going to Windows -> Show Views -> Other -> LogCat. Hope that helps. Sriram – Sriram Feb 02 '11 at 16:14
  • Ok with soundpool I ran into this error. well not an error but well let me explain. I tried both examples from this link. http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html and http://www.droidnova.com/creating-sound-effects-in-android-part-2,695.html and – BluMouse Feb 04 '11 at 13:51
  • @Sriram mp1.create(this, R.raw.age) == NULL) mp1.create(this,R.raw.age is underlined in yellow. options are change to static making mp1 into MediaPlayer, fix fifteen errors. add error supressing to onclick(). Null is underlined in red. options create constant, variable,field parameter, or change to mp1. what should I do? – BluMouse Feb 04 '11 at 14:10
  • @Sriram i was able to get rid of the yellow underline by changing it to mp1 = MediaPlayer.create(this, R.raw.age) but the Null is still underlined in red. – BluMouse Feb 04 '11 at 14:16
  • @Sriram I opened the logCat. I ran the project without the catch because As I said earlier the NULL was still underlined in red. So I ran it like you had it without the catch and try and I opened the logCat and it states java.LangNullPointerException package.Activity.onCreate(activity.java:71 – BluMouse Feb 04 '11 at 15:26
  • 02-04 15:21:56.273: ERROR/AndroidRuntime(322): at com.package.NounsActivity.onCreate(NounsActivity.java:71) 02-04 15:21:56.273: ERROR/AndroidRuntime(322): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 02-04 15:21:56.273: ERROR/AndroidRuntime(322): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459) – BluMouse Feb 04 '11 at 15:27
  • ok folks I added the soundpool try so you could see how that went. – BluMouse Feb 04 '11 at 15:50
0

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.

BluMouse
  • 5
  • 1
  • 6
  • If the question has been answered, you should mark it as such. Also, this isn't an answer. And don't delete your question after it's been answered, now no one knows what the answers are answering. – Falmarri Feb 05 '11 at 00:40
  • If an answer worked, you might want to mark it as the correct answer. It will help others who come across the thread. – Sriram Feb 10 '11 at 06:45