在ExoPlayer实现视频流截屏

在ExoPlayer使用播放视频流时,有时我们需要截取播放器中视频截屏,如果直接用下面代码:

publicstaticBitmapcapture(Activity activity){

activity.getWindow().getDecorView().setDrawingCacheEnabled(true);

Bitmap bmp = activity.getWindow().getDecorView().getDrawingCache();

returnbmp;

}

仅能得到页面静态内容的图像,SurfaceView的视频图像是截不到的,图像是黑屏。


解决方案:

Android中可以用TextureView替代SurfaceHolder/SurfaceView,然后用TextureView的getBitmap获取当前图像的截图,所以在ExoPlayer中用TextureView代替PlayerView即可支持视频的截图功能,即:

public boolean initView() {

//播放器控件初始化

        mTextureView =new TextureView(this.getContext());        RelativeLayout.LayoutParams layoutParams =new RelativeLayout.LayoutParams(this.getWidth(),this.getHeight());

        this.addView(mTextureView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

return true;

}


播放器构建代码:

mPlayer =

ExoPlayerFactory.newSimpleInstance(

/* context= */ this.getContext());

mPlayer.setPlayWhenReady(true);

mPlayer.setVideoTextureView(mTextureView);

mMediaSource = buildMediaSource(Uri.parse(“www.xxx.xxxx/xxx.m3u8”),null);

mPlayer.prepare(mMediaSource,true,false);


在需要截屏的地方调用mTextureView.getBitmap()获取截屏。另外,截屏是UI操作,需要在UI线程里调用mTextureView.getBitmap()。


参考链接:

https://github.com/google/ExoPlayer/issues/2993



注意:TextureView代替PlayerView,使ExoPlayer可以实现视频截屏,但在某些性能较差的智能电视中会造成视频页面播放异常[花屏]。

你可能感兴趣的:(在ExoPlayer实现视频流截屏)