我想一个接一个地播放不同的视频,就像一个流。切换视频时没有1秒延迟。我使用了SurfaceView
和MediaPlayer
。
如果我使用一个MediaPlayer
并更改视频,则可以正常工作,但切换视频会有一点延迟。这就是为什么我使用2个MediaPlayer
对象 - 当1正在玩其他对象时正在准备。但是当我开始它时,表面不显示视频。
这是我的代码:
vidSurface = (SurfaceView) findViewById(R.id.surfaceView);
try {
mediaPlayer = new MediaPlayer();
mediaPlayer2 = new MediaPlayer();
mediaPlayer.setDataSource(path);
mediaPlayer2.setDataSource(path2);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
//start next video here
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=null;
try {
mediaPlayer2.start();
}
catch(Exception e){}
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();
mediaPlayer2.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer2.prepare();
}
catch(Exception e){
e.printStackTrace();
}
mediaPlayer.start();
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
mediaPlayer.setDisplay(vidHolder);
mediaPlayer2.setDisplay(vidHolder);
//setup
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
}
@Override
public void onPrepared(MediaPlayer mp) {
//start playback
}