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.
android.media.MediaPlayer. MediaPlayer class can be used to control playback of audio/video files and streams. MediaPlayer is not thread-safe. Creation of and all access to player instances should be on the same thread. If registering callbacks, the thread must have a Looper.
Just use pause to stop, followed by seekTo(0) before restarting: mediaPlayer. seekTo(0); mediaPlayer.
Start/Pause the playback: After loading the media file, you can start playing the media file by using the start() method. Similarly, you can pause the playing media file by using the pause() method. Stop the playback: You can stop playback by using the reset() method.
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.
Add this:
mp.reset();
mp.setDataSource(MEDIA_PATH);
mp.prepare();
mp.start();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With