Sharjeel
Sharjeel

Reputation: 15798

Android Media Player Restart Audio After Calling Stop

I'm able to stream audio and stop it without any problem, but when I try to start it again after stop, it doesn't start and I get an IllegalState exception.

Here is what I'm doing:

Start Playing

mediaPlayer.setDataSource(PATH);
mediaPlayer.prepare();
mediaPlayer.start();

Stop Playing

mediaPlayer.stop

Now, if I want to start playing again the same media, what will I have to do?

*PATH is the URL of a continuous running radio station.

Upvotes: 13

Views: 22673

Answers (3)

abalter
abalter

Reputation: 10393

In case you don't have access to the data source in the current scope, you can do:

mp.pause();
mp.seekTo(0);

Then when you do

mp.start();

play will start from the beginning again.

I needed this because I had a button that toggled playing. I had a togglePlayer method in which the datasource was out of scope.

Upvotes: 41

Sandy
Sandy

Reputation: 6353

Add this:

mp.reset();
mp.setDataSource(MEDIA_PATH);
mp.prepare();
mp.start();

Upvotes: 11

Gao Yuesong
Gao Yuesong

Reputation: 144

you can check the state diagram of mediaplayer http://developer.android.com/reference/android/media/MediaPlayer.html after the mediaplayer stopped, must call prepare, when prepared,and then you can call start method.

Upvotes: 5

Related Questions