我正在尝试通过无缝转换循环播放相同的视频时缩小间隙/暂停。在onCompletion中setLooping和重新启动视频都会导致明显的暂停。我注意到有一个setNextMediaPlayer()方法,它是为无间隙过渡到下一个MediaPlayer而创建的。它已成功用于音频。但是对于视频,表面一次只能设置为一个mediaPlayer。我设法让它工作的唯一方法是将第一个mediaPlayer的表面设置为null,并将表面设置为第一个mediaPlayer的onCompletion中第二个mediaPlayer的实际表面。但是,这会引入自己的暂停/间隙,并不比使用一个mediaPlayer更好。
一些代码:
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Utils.logDebug(TAG, "mediaPlayer.onPrepared");
if (mediaPlayer != null) { // && visible) { // check
if (surface != null && powerManager.isScreenOn()) {
mediaPlayer.setSurface(surface);
}
}
mediaPlayer2.setNextMediaPlayer(mediaPlayer);
currentMediaPlayer = mediaPlayer;
seekPlayerToCurrentPosition();
startPlayer();
}
});
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Utils.logDebug(TAG, "mediaPlayer.onCompletion");
currentMediaPlayer.setSurface(null);
currentMediaPlayer = mediaPlayer2;
currentMediaPlayer.setSurface(surface);
}
});
// MEDIA PLAYER 2
mediaPlayer2.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
Utils.logDebug(TAG, "mediaPlayer2.onPrepared");
mediaPlayer.setNextMediaPlayer(mediaPlayer2);
}
});
mediaPlayer2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Utils.logDebug(TAG, "mediaPlayer2.onCompletion");
currentMediaPlayer.setSurface(null);
currentMediaPlayer = mediaPlayer;
currentMediaPlayer.setSurface(surface);
}
});