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?

Community
  • 1
  • 1
Den
  • 255
  • 1
  • 3
  • 8
  • 2
    Make sure the sound you're playing IS longer than 5 seconds, first :) – Gabriel Negut Jun 05 '11 at 08: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 Jun 05 '11 at 09: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 Sep 14 '11 at 06:32

3 Answers3

81

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();
}
dac2009
  • 3,521
  • 1
  • 22
  • 22
  • 7
    This fixed my instance of this problem. Kinda nuts that the MediaPlayer can get gc'd during playback. – Reuben Scratton Dec 17 '11 at 15:11
  • 1
    Very interesting. Great way to explain how garbage collection works :) –  Dec 22 '14 at 08: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. – user1608385 Dec 11 '18 at 00:20
  • setLooping method no longer exists – Gerry Dec 29 '18 at 21:49
  • not working in latest devices. Can you please help me in other ways. Facing this issue for Online Stream URL's. – aswanth bonthala Feb 05 '21 at 16:35
  • curious if you know how to make a media player follow the android systems alarm volume instead of the media volume. I am trying to make alarms that go off that follow the volume of the alarm slider, which seems to be hidden now. so I dont even know where to begin. – auotive Jul 23 '22 at 05:28
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();
        }
     });
Afshin Izadi
  • 527
  • 1
  • 6
  • 13
-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());
ktsakiris
  • 59
  • 1
  • 10
  • You can not just assume the player will be ready to play after the duration of the file. What if you want it to start after it has been prepared. See here: https://developer.android.com/guide/topics/media/mediaplayer – John Glen Jan 04 '23 at 23:33