Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop media player properly , android

I have created a list of songs on click on the song i am able to play the song using MedaiPlayer. While one song is playing if the user clicks another song then i am stopping the media player and starting the player again. But I am getting illegalstateexception in reset(). Here is the code where I am getting the exception. How to stop a player properly? also why am i getting this exception. How to avoid it?

public void stopPlayer() {
        try {
            if (player != null) {
                // Log.e("Trying to Stop "," Player ");
                player.stop();
                player.release();

                player.reset();// causes IllegalstateException

                player = null;

            }

        } catch (Exception e) {
            player = null;
            playerStatus = false;
            e.printStackTrace();
        }
    }
like image 295
Sunil Kumar Sahoo Avatar asked Nov 22 '11 08:11

Sunil Kumar Sahoo


People also ask

How do I turn off MediaPlayer on Android?

1 Answer. Show activity on this post. MediaPlayer has an OnCompletionListener callback you can register to get notified when playback stops.

What is Android MediaPlayer?

MediaPlayer Class in Android is used to play media files. Those are Audio and Video files. It can also be used to play audio or video streams over the network.

What is the MediaPlayer class?

MediaPlayer. This class is the primary API for playing sound and video. AudioManager. This class manages audio sources and audio output on a device.


1 Answers

try this :

player.reset();
player.release();

and also have a look at media player state diagram.

like image 111
Pawan Avatar answered Sep 23 '22 11:09

Pawan