Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

java - Android: MediaPlayer Play, Pause, Resume audio file like WhatsApp

I'm working on one project, I tried to play audio files like WhatsApp. suppose three files in a row sent by the user, ** I want to play/pause/resume audio file exactly like WhatsApp** -

what I want to do:

if I clicked on the first audio file (play button) it should start playing if I clicked on pause it should stop and if I clicked on play again it should start playing from exactly where we paused(resume audio file).

other scenarios would be if the first file is playing and I clicked on the second file, the first file should stop playing(rather say pause) and the second file should start playing the second audio file, and if I clicked on play of third file second file should pause and the third audio file should start playing. and if I click play on first file third file should pause and the first file should resume playing.

what done by me: I'm using MediaPlayer to play the audio file in recylerview. I can play and pause files but I fail to resume file after a pause(how to store the last position of each file) and when my first file playing and I clicked on second file both file continue playing rather than the recent one.

tried: I tried to find out this on the stack but there are lots of questions like on play pause and resume none of them help me to solve my problem half of the questions unanswered and half of the users no longer using stack overflow(last seen).

my question: how to resume each file after a pause, how to save the last position?

code: I'm using MVVM architecture and my code written in one of the view holders.

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.audio_play_button:
                mAudioPauseButton.setVisibility(View.VISIBLE);
                mAudioPlayButton.setVisibility(View.INVISIBLE);
                play();
                break;
            case R.id.audio_pause_button:
                mAudioPauseButton.setVisibility(View.INVISIBLE);
                mAudioPlayButton.setVisibility(View.VISIBLE);
                pause();
                break;
            default:
                break;
        }
    }

i'm using two icons for play pause and handle visibility on click.

 private void play() {

        if (mMediaPlayer == null) {
            mMediaPlayer = new MediaPlayer();
            setAudioUrltoMediaPlayer();

        } else {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            } else {
                    setAudioUrltoMediaPlayer();
            }
        }

        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                if (mMediaPlayer != null) {

                    mMediaPlayer.start();
                    totalTime = mMediaPlayer.getDuration();
                    mTextChatAudioTime.setText(millisecondsToString(totalTime));
                    mAudioSeekBar.setProgress(0);
                    mAudioSeekBar.setMax(totalTime);
                    mAudioSeekBar.setClickable(true);

                    new Timer().scheduleAtFixedRate(new TimerTask() {
                        @Override
                        public void run() {
                            mAudioSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
                        }
                    }, 0, 200);

                    mAudioSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                            if (fromUser) {
                                mMediaPlayer.seekTo(progress);
                            }
                            final long mMinutes = (progress / 1000) / 60;//converting into minutes
                            final int mSeconds = ((progress / 1000) % 60);//converting into seconds
                            mTextChatAudioTime.setText(mMinutes + ":" + mSeconds);
                        }

                        @Override
                        public void onStartTrackingTouch(SeekBar seekBar) {
                        }

                        @Override
                        public void onStopTrackingTouch(SeekBar seekBar) {
                            int CurrentLevel = mAudioSeekBar.getProgress();
                            if (CurrentLevel < 30)
                                CurrentLevel = 30;
                            mAudioSeekBar.setProgress(CurrentLevel);
                        }
                    });

                    mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            final Handler handler = new Handler();
                            handler.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    mAudioPauseButton.setVisibility(View.INVISIBLE);
                                    mAudioPlayButton.setVisibility(View.VISIBLE);
                                    mMediaPlayer.stop();

                                }
                            }, 200);
                        }
                    });

                }
            }
        });

    }

    private void setAudioUrltoMediaPlayer() {
        if (setAudioUrl != null) {
            Log.d("audio file", "available" + setAudioUrl);
            try {
                Log.d("setAudioUrl", ">>>>>>>>>>>>>>>>>>>>>" + setAudioUrl);
                mMediaPlayer.reset();
                mMediaPlayer.setDataSource(setAudioUrl);
                mMediaPlayer.prepare();
                mMediaPlayer.setVolume(1.0f, 1.0f);
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            Log.d("audio file", "available" + setAudioUrl);
        }
    }

    private void pause() {
        if (mMediaPlayer != null) {
            if (mMediaPlayer.isPlaying()) {
                //lastposition = mMediaPlayer.getCurrentPosition();
                mMediaPlayer.pause();
            }
        }
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This is the mediaplayer I had written a few years back.

This has an option to play a playlist. It has Play, Pause, Next, Prev buttons and their actions too.

You can modify the app as per your need. You can show your playlist in a recyclerview and then on item click you can send the item as a broadcast to play.

Let me know if you need more help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...