Gapless Playback with android MediaPlayer

守給你的承諾、 提交于 2019-12-06 06:37:50

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/15523788/gapless-playback-with-android-mediaplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!