9

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();



                }
            }
1
  • I guess I should be more clear.I want to stop the stream and then restart at the live position. Sep 19, 2012 at 0:55

7 Answers 7

21

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.

1
  • From memory, just start it again. You don't need to seek.
    – David
    Apr 14, 2016 at 13:35
9

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.

7
public void playButton (View view){

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

Works OK with me.

0
1

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();
            }
        }
0

You must be call onDestroy() medthod when finish you song / play song. You get help from my real time code.

public class RingtonePlayingService extends Service {
    public static MediaPlayer mediaPlayer;
    public static Context context;

    @SuppressWarnings("deprecation")
    @Override
    public void onCreate() {
        super.onCreate();

        context = getApplicationContext();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
            mediaPlayer = new MediaPlayer();
            // this is important.
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);

            try {
                // Uri ringerUri = RingtoneManager.getActualDefaultRingtoneUri(
                // getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
                mediaPlayer.setDataSource(context,
                        Settings.System.DEFAULT_RINGTONE_URI);
                // mediaPlayer.setDataSource(context, ringerUri);

                mediaPlayer.setLooping(true);
                mediaPlayer.prepare();
                if (SettingsManager.getInstance(context).getFinishCall()) {

                } else {
                    mediaPlayer.start();
                }

            } 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();
            }


        return START_NOT_STICKY;
    }


    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        TTcall.onDestroyr();
        if (mediaPlayer != null) {

            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
                mediaPlayer.stop();
                mediaPlayer = null;
            }

        }
    }

}

I expected you will be helpful from above code. please try using like this. Thanks

-2

Also you likely don't need to stop(), pause() would likely suffice, meaning that you can call play() once you want to continue playing.

1
  • OP wants to start over, not keep playing from where left off.
    – abalter
    Jun 1, 2014 at 0:12
-5
mediaPlay.seek(0);
mediaPlay.pause();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.