Android Media Player Replay Problem

Keywords: MediaPlayer

Project needs to do a voice IM function, voice playback using MediaPlayer implementation, when a quick click on voice, an audio playback does not end, back and forth play, will reproduce the problem of repeat play, the following explains how to solve.

private MediaPlayer mMediaPlayer;
private File mLastFile = null;

//Unified Method of Playing Audio
private void doPlay(File mAudioFile) {
    //Configure Player MediaPlayer
    LogUtil.d("ChatWebViewActivity", "in doPlay");
    if (null != mMediaPlayer) {
        if (mMediaPlayer.isPlaying()) {
            if (!mLastFile.getName().equals(mAudioFile.getName())) {
                //Unlike the same file, pause the last file and play the next audio file.
                stopPlay();
            } else {
                //For the same file, click again and then pause directly.
                stopPlay();
                return;
            }
        }
    }
    LogUtil.d("ChatWebViewActivity", "in doPlay notFirst");
    mLastFile = mAudioFile;
    mMediaPlayer = new MediaPlayer();
    try {
        //Setting Sound File
        FileInputStream fis = new FileInputStream(mAudioFile);
        FileDescriptor fileD = fis.getFD();
        mMediaPlayer.setDataSource(fileD);
        mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mMediaPlayer.start();
            }
        });
        //Setting up a listening callback
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                voiceStatusChange(false, voiceId);
                stopPlay();
            }
        });

        //Setting up an error listener
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                //Prompt users
                stopPlay();
                //Release player
                return true;
            }
        });

        //Configure volume, whether or not to cycle
        mMediaPlayer.setVolume(1, 1);
        mMediaPlayer.setLooping(false);
        mMediaPlayer.prepareAsync();
    } catch (Exception e) {
        e.printStackTrace();
        stopPlay();
    }
}

public void stopPlay() {
    if (mMediaPlayer != null) {
        try {
            mMediaPlayer.stop();
        } catch (Exception e) {
            e.printStackTrace();
            mMediaPlayer = null;
            mMediaPlayer = new MediaPlayer();
            mMediaPlayer.stop();
        }

        mMediaPlayer.release();
        mMediaPlayer = null;
    }
}

The solution is to record the audio files that were played last time. Whether they are the same file or not is judged when playing each time. If they are different audio files, the audio files that were played last time need to be suspended first, and the new audio files need to be played.

Posted by macastor on Sat, 05 Oct 2019 17:19:25 -0700