Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mediaplayer error (-19,0) after repeated plays

I have a game in which a sound plays when a level is completed. Everything works fine to start with but after repeating a level 10 or 20 times the logcat suddenly reports: "MediaPlayer error (-19,0)" and/or "MediaPlayer start called in state 0" and the sounds are no longer made.

I originally had the all sounds in mp3 format but, after reading that ogg may be more reliable, I converted them all to ogg, but the errors appeared just the same.

Any idea how I can fix this problem?

like image 213
Mick Avatar asked Mar 27 '12 11:03

Mick


4 Answers

I was getting the same problem, I solved it by adding the following code to release the player:

mp1 = MediaPlayer.create(sound.this, R.raw.pan1);
mp1.start();
mp1.setOnCompletionListener(new OnCompletionListener() {
    public void onCompletion(MediaPlayer mp) {
        mp.release();

    };
});
like image 154
Gabriel Kaffka Avatar answered Nov 17 '22 20:11

Gabriel Kaffka


I think you are not releasing the mediaplayers you are using to play the sound.. You need to release() the media players otherwise the resources are not released , and you soon get out of memory (since you allocate them again next time). so,I think you can play twice or even thrice... but not many times without releasing the resources

like image 29
5hssba Avatar answered Nov 17 '22 21:11

5hssba


MediaPlayer is not a good option when you are playing small sound effects as the user can click on multiple buttons very soon and you will have to create a MP object for all of them which doesnt happen synchronously. That is why you are not hearing sounds for every click. Go for the SoundPool Class which allows you to keep smaller sounds loaded in memory and you can play them any time you want without any lag which you would feel in a mediaplayer. http://developer.android.com/reference/android/media/SoundPool.html Here is a nice tutorial : http://www.anddev.org/using_soundpool_instead_of_mediaplayer-t3115.html

like image 16
Akhil Avatar answered Nov 17 '22 20:11

Akhil


I solved both the errors (-19,0) and (-38,0) , by creating a new object of MediaPlayer every time before playing and releasing it after that.

Before :

void play(int resourceID) {
    if (getActivity() != null) {

         //Using the same object - Problem persists
        player = MediaPlayer.create(getActivity(), resourceID);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.release();
            }
        });

        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });

    }
}

After:

 void play(int resourceID) {

    if (getActivity() != null) {

        //Problem Solved
        //Creating new MediaPlayer object every time and releasing it after completion
        final MediaPlayer player = MediaPlayer.create(getActivity(), resourceID);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);

        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                player.release();
            }
        });

        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();
            }
        });

    }
}
like image 2
Saurabh Padwekar Avatar answered Nov 17 '22 22:11

Saurabh Padwekar