21

I'm currently developing a simple game and now it's time to add music and sound effect. I tried using MediaPlayer, just like described here: Android media player bug

However I have another problem, the MediaPlayer stop playing the music after about 5 seconds. What is probable causing this?

3
  • 2
    Make sure the sound you're playing IS longer than 5 seconds, first :) Commented Jun 5, 2011 at 8:17
  • You probably should show us some MediaPlayer related code, and also use setOnErrorListener(...) and setOnCompletionListener(...) to see maybe error occurs or playback is just completed successfully.
    – inazaruk
    Commented Jun 5, 2011 at 9:41
  • We had simialr problems with playing online streams with MediaPlayer from Android 2.3. Our stream was about 2 minutes long. Our conclusion was that this was likely a bug in Android r concrete type of stream or video format.
    – ATom
    Commented Sep 14, 2011 at 6:32

3 Answers 3

82

I had this problem too. It was probably due to having the MediaPlayer-object only existing within a method.

For example:

//ERROR, stops after 5 sec!
public static void playMusic(int id)
{
  MediaPlayer mediaPlayer = MediaPlayer.create(context, id);
  mediaPlayer.setLooping(true);
  mediaPlayer.start();
}

It is most likely that the garbage collector will come in and clean away the MediaPlayer-object.

This fixed the error for me:

//mediaPlayer-object will not we cleaned away since someone holds a reference to it!
private static MediaPlayer mediaPlayer;

public static void playMusic(int id)
{
    mediaPlayer = MediaPlayer.create(context, id);
    mediaPlayer.setLooping(true);
    mediaPlayer.start();
}
6
  • 9
    This fixed my instance of this problem. Kinda nuts that the MediaPlayer can get gc'd during playback. Commented Dec 17, 2011 at 15:11
  • 1
    Very interesting. Great way to explain how garbage collection works :)
    – user3011902
    Commented Dec 22, 2014 at 8:00
  • 1
    Best not to make it static, otherwise you are setting yourself up for memory leaks. You can always pass a reference to the activity to your static playMusic(int) function to access the MediaPlayer or make your playMusic(int) function not static. Commented Dec 11, 2018 at 0:20
  • setLooping method no longer exists
    – Gerry
    Commented Dec 29, 2018 at 21:49
  • not working in latest devices. Can you please help me in other ways. Facing this issue for Online Stream URL's. Commented Feb 5, 2021 at 16:35
0

In my case that was just because using MediaPlayer in a Dialog and right after playing the sound called dismiss() function therefore the garbage collector would eliminate the MediaPlayer object and it cause MediaPlayer stop working, a better way to play sound in such situation is to play it in the parent class instead of playing it in the Dialog like as this

/* declare myAudio as public */

myAudio = MediaPlayer.create(this, R.raw.my_audio);
CustomDialog cdd = new CustomDialog(MainActivity.this);
cdd.show();
Button playMyMediaSoundBtn = cdd.findViewById(R.id.play_button);
playMyMediaSoundBtn.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
            myAudio.start();
            cdd.dismiss();
        }
     });
-1

You should create an asychronous code to let media player play what he has to play in the background. Something like this:

final MediaPlayer mp = MediaPlayer.create(MainActivity.this, R.raw.nomarxia);


                Handler mHandler = new Handler();
                mHandler.postDelayed(new Runnable() {
                    public void run() {
                        mp.start();
                    }
                }, mp.getDuration());
1

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.