MediaPlayer stop playing after about 5 seconds
Asked Answered
G

3

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?

Gregor answered 5/6, 2011 at 8:9 Comment(3)
Make sure the sound you're playing IS longer than 5 seconds, first :)Deity
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.Errick
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.Nationwide
B
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();
}
Bayonet answered 10/10, 2011 at 22:15 Comment(6)
This fixed my instance of this problem. Kinda nuts that the MediaPlayer can get gc'd during playback.Jube
Very interesting. Great way to explain how garbage collection works :)Prejudicial
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.Rebatement
setLooping method no longer existsNettienetting
not working in latest devices. Can you please help me in other ways. Facing this issue for Online Stream URL's.Oberammergau
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.Flimsy
S
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();
        }
     });
Stipulation answered 29/7, 2023 at 7:5 Comment(0)
A
-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());
Asch answered 30/1, 2020 at 15:1 Comment(1)
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: developer.android.com/guide/topics/media/mediaplayerAryl

© 2022 - 2024 — McMap. All rights reserved.