Prabha Karan
Prabha Karan

Reputation: 1329

How to resume the audio where it is paused?

I am facing the issue in playing the audio where it is paused,i want to perform three functions in a single play button.If initially play button pressed means audio should play.If second time play button pressed means the audio should pause and again play button pressed means the audio should resume from the pause state.I have tried many methods i can pause audio but again it is playing from the beginning not resuming the audio from pause condition.

I used this condition if( !mediaPlayer.isPlaying() && mediaPlayer.getCurrentPosition() > 0 ) but it is not working.

My working code is

    play.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Toast.makeText(activity, "Playing sound", Toast.LENGTH_SHORT).show();
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.pause();
            }
            if( !mediaPlayer.isPlaying() && mediaPlayer.getCurrentPosition() > 0 ){
                long currentPosition = mediaPlayer.getCurrentPosition();
                mediaPlayer.seekTo((int) currentPosition);
                mediaPlayer.start();
            }

            mediaPlayer.reset();
            try {
                mediaPlayer.setDataSource(path);
                mediaPlayer.setOnPreparedListener((MediaPlayer.OnPreparedListener) activity);
                mediaPlayer.prepare();
            } catch (IOException e) {
                e.printStackTrace();
            }
            mediaPlayer.start();

            finalTime = mediaPlayer.getDuration();
            startTime = mediaPlayer.getCurrentPosition();

            if (oneTimeOnly == 0) {
                seekBar.setMax((int) finalTime);
                oneTimeOnly = 1;
            }

            songEndTime.setText(String.format("%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours((long) finalTime),
                    TimeUnit.MILLISECONDS.toMinutes((long) finalTime) -
                            TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours((long) finalTime)), // The change is in this line
                    TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) finalTime))));

            songStartTime.setText(String.format("%02d:%02d:%02d",
                    TimeUnit.MILLISECONDS.toHours((long) startTime),
                    TimeUnit.MILLISECONDS.toMinutes((long) startTime) -
                            TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS.toHours((long) startTime)),
                    TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
                            TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes((long) startTime))));



            seekBar.setProgress((int) startTime);
            myHandler.postDelayed(UpdateSongTime, 100);
            stop.setVisibility(View.VISIBLE);
            play.setVisibility(View.GONE);
        }
    });

Upvotes: 1

Views: 690

Answers (2)

BluRe.CN
BluRe.CN

Reputation: 311

Set the MediaPlayer DataSource first and then remove "mediaPlayer.reset();" because its resetting the MediaPlayer Object each time the button is clicked. That's the reason why it'll always start from the beginning.

Upvotes: 0

Jaydeep Khambhayta
Jaydeep Khambhayta

Reputation: 5349

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

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

mediaPlayer.start();    
mediaPlayer.seekTo(length);

Upvotes: 0

Related Questions