Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop MediaPlayer stream and then restart it? Android

I am making a simple app to play a online radio stream. I know how to set up the stream. Here is my code so far. It sets up the stream and plays it fine. But if I hit stop, I cannot get it to start again. Am I missing something obvious?

final MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    try {
        mediaPlayer.setDataSource(url);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        mediaPlayer.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } // might take long! (for buffering, etc)

   streamButton = (ToggleButton) findViewById(R.id.playstopstream);

   streamButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton arg0, final boolean isChecked) {
        // TODO Auto-generated method stub
         new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                if(isChecked)
                {


                    mediaPlayer.start();

                }
                else
                {
                    mediaPlayer.stop();
                    mediaPlayer.reset();



                }
            }
like image 764
wilxjcherokee Avatar asked Sep 19 '12 00:09

wilxjcherokee


4 Answers

public void playButton (View view){

        if (isActive == false){
            mediaPlayer.start();
            isActive = true;
        }else{
            mediaPlayer.seekTo(0);
            mediaPlayer.start();
        }
    }

Works OK with me.

like image 75
David Kamal Avatar answered Nov 06 '22 01:11

David Kamal


Old question, but I came across it looking for this. Just use pause to stop, followed by seekTo(0) before restarting:

mediaPlayer.seekTo(0);
mediaPlayer.start();

Works without having to prepare the recorder again every time.

like image 36
David Avatar answered Nov 06 '22 02:11

David


When you call mediaPlayer.reset() mediaPlayer goes to IDLE state. For playback, you should do initialize to prepare before start.

You can check the mediaplayer state here: http://developer.android.com/reference/android/media/MediaPlayer.html.

like image 24
user1677038 Avatar answered Nov 06 '22 01:11

user1677038


Try this

@Override
public void onCheckedChanged(CompoundButton arg0, final boolean isChecked) {
    // TODO Auto-generated method stub
     new Thread(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            if(isChecked) {
                mediaPlayer.setDataSource(url);
                mediaPlayer.prepare();
                mediaPlayer.start();
            }
            else {
                mediaPlayer.stop();
                mediaPlayer.reset();
            }
        }
like image 33
Munawir Avatar answered Nov 06 '22 01:11

Munawir