How to resume the mediaplayer?

耗尽温柔 提交于 2019-11-27 06:54:38
Prateek Raj

Thank you for your attention but I've got it myself

for pausing the Mediaplayer I used:

Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();

and for resuming the player from the position where it stopped lately is done by:

Mediaplayer.seekTo(length);
Mediaplayer.start();

I think you should go through the documentation found here: http://developer.android.com/reference/android/media/MediaPlayer.html

Some quotes from the docs:

Playback can be paused and stopped, and the current playback position can be adjusted. Playback can be paused via pause(). When the call to pause() returns, the MediaPlayer object enters the Paused state. Note that the transition from the Started state to the Paused state and vice versa happens asynchronously in the player engine. It may take some time before the state is updated in calls to isPlaying(), and it can be a number of seconds in the case of streamed content.

  • Calling start() to resume playback for a paused MediaPlayer object, and the resumed playback position is the same as where it was paused. When the call to start() returns, the paused MediaPlayer object goes back to the Started state.
  • Calling pause() has no effect on a MediaPlayer object that is already in the Paused state.

States explained:

And quote from the start() method of the MediaPlayer

public void start ()

Starts or resumes playback. If playback had previously been paused, playback will continue from where it was paused. If playback had been stopped, or never started before, playback will start at the beginning.

So to answer your question directly, to resume the paused MediaPlayer instance from the point where it was paused use start() again on that instance.

Hoven

In the accepted answer, the correct order is:

Mediaplayer.start();
Mediaplayer.seekTo(length);

in case you use two Buttons one for play and one for pause then the below code is working and tried:

                playbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mPlayer = MediaPlayer.create(MainActivity.this,         
                    R.raw.adhan);

                    if (mPlayer.isPlaying()) {


                    } else {
                        mPlayer.seekTo(length);
                        mPlayer.start();
                      }

                }
                });

               pausebtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    mPlayer.pause();
                    length = mPlayer.getCurrentPosition();


                  }
                });

When using seekTo I found it often starts playing with the very start of the track then carries on playing from the point of where seekTo was set.

What worked for me was muted the volume and then crank it back up once the currentProgress matches the current seek (stored in global).

   public class MainActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer;
   public void play(View view) {
           mediaPlayer.start();
   }
   public void pause(View view){
           mediaPlayer.pause();
   }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer= MediaPlayer.create(this,R.raw.someaudio);
    }
}

Make two buttons for play and pause. And use this code. It worked for me.

what about this way?

     package com.mycompany.audiodemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.media.MediaPlayer;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    MediaPlayer mediaPlayer=null;
    int playPosition=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mediaPlayer = MediaPlayer.create(this,R.raw.sampleaudio);
    }

    public void playAudio(View view){

        //mediaPlayer.seekTo(playPosition);
        mediaPlayer.start();

    }

    public void pauseAudio(View view){
        mediaPlayer.pause();
        //playPosition =  mediaPlayer.getCurrentPosition();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!