Gapless Playback with android MediaPlayer

2020-07-23 04:05发布

I'm trying to repeatedly play a audio continuously, without any gap. I've tried,

mediaplayer.setLooping(true);

But it gives a gap between repeat time. And tried this,

mediaplayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener()  {
    @Override
    public void onCompletion(MediaPlayer mp)
    {
        mediaplayer.start();
    }
});

It gives a gap also. I've tried with different audio format. But neither way worked for me. Is there any other way in android?

2条回答
成全新的幸福
2楼-- · 2020-07-23 04:44

Probably MediaPlayer is re-prefetching the data from server (or even from the device itself), this may take some seconds. Only if the buffering is done, it starts playing again.
If you want to bypass this, you can AFAIK only use a second MediaPlayer which starts buffering the media file shortly before the first one stops playing. Then you can start the second one on OnCompletionListener.

查看更多
趁早两清
3楼-- · 2020-07-23 04:47

When a playback finishes, the player engine would check if all tracks are completed and once done, it will check for the looping flag. Based on the looping flag, a seek to 0 seconds. As part of the seek, the player engine will read the data from the specific position and start the playback.

The delay may be due to the seek implementation along with the delay introduced by the storage medium like sdcard apart from re-initializing all tracks and restarting them. Hence, there is definite delay by the time the player reverts back to the starting position.

The underlying looping implementation can be found in this function AwesomePlayer::onStreamDone as shown here: http://androidxref.com/4.2.2_r1/xref/frameworks/av/media/libstagefright/AwesomePlayer.cpp#834

EDIT 1:

To implement a true gapless playback, you could also consider the setNextMediaPlayer() feature.

查看更多
登录 后发表回答