2

I have a sound bite that plays on a click of a button (of 2 secs). On completion i have called a new activity and released the Media Player.

But my app crashes at this.

My code

final ImageButton TtriangleButton=(ImageButton)findViewById(R.id.triangleTriangle);
    TtriangleButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            /* Code for playing the "Very Good" sound */    
            final MediaPlayer mediaPlayer = MediaPlayer.create(shapeTriangleActivity.this, R.raw.applause);
            mediaPlayer.start();

            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

                @Override
                public void onCompletion(MediaPlayer mp) {

                    Intent myintent1 = new Intent(shapeTriangleActivity.this,LearningTimeMenu.class);
                    myintent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(myintent1);
                    mediaPlayer.release();

                }});
        }});

One MORE issue that i have is, at the release i have tried to add a NULL as well mediaPlayer = null; -- but i get a compilation error like "The final local variable mediaPlayer cannot be assigned, since it is defined in an enclosing type".

Am stuck on this. Need help.

Updated with Error Log

10-31 14:54:45.800: E/AndroidRuntime(859): android.content.res.Resources$NotFoundException: Resource ID #0x7f040006

Also my files are .wav files. I have noticed that .mp3 files do not have this issue, even though they both are the same file size.

1
  • why do you try to null your variable ?
    – njzk2
    Oct 31, 2012 at 9:40

3 Answers 3

3

Release media player before starting a new activity like this ::

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.release();
                Intent myintent1 = new Intent(shapeTriangleActivity.this,LearningTimeMenu.class);
                myintent1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(myintent1);


            }});
1
  • 1
    Try to set media player as null after release it and also make one change that create media player instance globally .Hope it may help you :) Oct 31, 2012 at 9:36
3

Try it this way......

- You should release it, and then assign null after completion of the playing.

- Then again initialize the media player when you want to play another sound.

 mPlayer.start();
 mPlayer.setOnCompletionListener(new OnCompletionListener() {

 @Override
public void onCompletion(MediaPlayer mp) {

             mPlayer.release();  // 
             mPlayer = null;

        }
});

- If you are playing only a 2sec sound clip, please use SoundPool to do this, as SoundPool is apt for these kinda sounds-clips....

Here are few limitations of SoundPool:

- Sound file should Not exceed 1 MB.

- Best result are obtained with .wav sound files, its even ok with .mp3, .mp4, .m4a

- Please go with bitrate of 128kb/sec and 22050KHz sampling rate, for complete playing of the sound-clip

3
  • In the above code - when i insert mPlayer = null; --> it gives me a compilation error of -> "The final local variable mediaPlayer cannot be assigned, since it is defined in an enclosing type". Any idea why this is?
    – Jasma
    Oct 31, 2012 at 12:25
  • @Jasma Declare the mPlayer which is of type MediaPlayer at Class scope. Oct 31, 2012 at 17:08
  • I can confirm that not setting MediaPlayer object = null to force immediate garbage collection can result in IllegalStateErrors later. In my case, I released the player in my activity's onDestroy() method but when I re-started the app I would get the error. Setting it to null after release() fixed it. Mar 17, 2013 at 1:06
1

First stop the media player and then release it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.