How to resume the mediaplayer?

Viewed 51777

I am using a media player.

I have the option for starting ,stopping and pausing the player. The problem I have is that I cannot find the option to resume the song from the point that it previously was paused..

Any help provide would be really helpful.

6 Answers

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


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