最近公司个广告机的项目要实现几段视频的无缝切换播放功能,我在网上找了一圈,发现网上说能实现实现视频无缝切换功能的都是用的surfaceview+mediaplayer,于是我开始漫漫的爬坑之路。
先简单介绍下surfaceview和mediaplayer,surfaceview有两级缓存机制,在播放着这帧视频的时候,会将下帧视频缓存到内存里,能够很好的保证视频播放的流畅性。mediaplayer是安卓的一个原生类,接下来的很多坑都是关于mediaplayer的。先看下mediaplayer的生命周期:
首先我们实例化两个mediaplayer对象:
>MediaPlayer mCurrentMediaPlayer, mNextMediaPlayer;
>mCurrentMediaPlayer = new MediaPlayer();
>mNextMediaPlayer = new MediaPlayer();
此时的mCurrentMediaPlayer和mNextMediaPlayer对象还没有被赋予资源,处于生命周期的Idle状态接着我们可以用setDataSource()来给MediaPlayer对象设置资源:
>mCurrentMediaPlayer.setDataSource(SubWayPlayerActivity.this, Uri.parse(path));
MediaPlayer对象被赋予资源之后,就处于Initialized状态,在此状态调用prepare()+start()方法即可播放视频。我选用的是prepareAsync()即异步装载方式,然后需要在OnPreparedListener监听中调用start方法来启动视频播放。而mediaplayer的reset()方法可以释放mediaplayer的资源,将对象初始化到Idle状态。
实现视频无缝切换功能的基本思路是:先将mCurrentMediaPlayer对象播放在mVideoSurface上,然后用getDuration()方法获取这段视频的时长,在mCurrentMediaPlayer中的视频快播放完毕时,用mNextMediaPlayer对象加载下一段视频,加载完毕时,调用start方法显示出来,然后把mCurrentMediaPlayer的资源释放,隐藏mVideoSurface。如此循环。
下面贴出surfaceview的初始化代码:
mVideoSurface = new SurfaceView(this);
mVideoSurface.getHolder().addCallback(new VideoSurfaceHodlerCallback());
mNextSurface = new SurfaceView(this);
mNextSurface.getHolder().addCallback(new NextVideoSurfaceHodlerCallback());
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.LEFT | Gravity.TOP;
mVideoSurface.setLayoutParams(lp);
FrameLayout.LayoutParams lps = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT);
lp.gravity = Gravity.LEFT | Gravity.TOP;
mNextSurface.setLayoutParams(lps);
mFrame.addView(mNextSurface);
mFrame.addView(mVideoSurface);
接下来要实现surfaceview的三个回调函数:
接下来贴出实现切换的线程代码:
class VideoSurfaceHodlerCallback implements SurfaceHolder.Callback {
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { }
@Override
public void surfaceCreated(SurfaceHolder holder){
mCurrentMediaPlayer.setDisplay(mVideoSurface.getHolder());
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) { } }
Runnable mPlayRun = new Runnable() {
@Override
public void run() {
String path = paths[mIndex % paths.length];
try { if (mIndex % 2 == 0) { mIndex++; mCurrentMediaPlayer.reset();
mVideoSurface.setVisibility(View.VISIBLE);
mCurrentMediaPlayer.setDataSource(MainActivity.this, Uri.parse(path));
mCurrentMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer arg0) {
mCurrentMediaPlayer.start();
mNextMediaPlayer.reset();
mNextSurface.setVisibility(View.INVISIBLE);
mHandler.postDelayed(mPlayRun, mCurrentMediaPlayer.getDuration()-230); } });
mCurrentMediaPlayer.prepareAsync();
} else {
mIndex++; mNextMediaPlayer.reset();
mNextSurface.setVisibility(View.VISIBLE);
mNextMediaPlayer.setDataSource(MainActivity.this, Uri.parse(path));
mNextMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override
public void onPrepared(MediaPlayer arg0) {
mNextMediaPlayer.start(); mCurrentMediaPlayer.reset();
mVideoSurface.setVisibility(View.INVISIBLE);
mHandler.postDelayed(mPlayRun, mNextMediaPlayer.getDuration()-230); } });
mNextMediaPlayer.prepareAsync();
} } catch (Exception e) {
e.printStackTrace(); } } };
接下来贴出初始化方法代码:
private void VideoInit() {
mHandler = new Handler();
mCurrentMediaPlayer = new MediaPlayer();
mNextMediaPlayer = new MediaPlayer();
mVideoSurface.getHolder().addCallback(new VideoSurfaceHodlerCallback());
mNextSurface.getHolder().addCallback(new NextVideoSurfaceHodlerCallback());
mHandler.postDelayed(mPlayRun, 1000); }
以上代码就是实现无缝切换视屏的全部代码了。
下面要说的坑才是重点:
①mediaplayer有个setNextMediaPlayer()方法,能设置下个播放的mediaplayer对象,但是我试了很多次,都没有办法使用这个方法。
②我虽然实现了视频的无缝切换,但是mNextMediaPlayer对象播放出来的视频始终与屏幕成90°,也就是说这种方法有很严重的bug的。只有在加载mNextMediaPlayer之前释放mCurrentMediaPlayer对象的资源,mNextMediaPlayer的视频才能正确播放,然而这又无法实现视频的无缝切换。
综上,我这次尝试失败了。