首页 > 解决方案 > 使用 VideoFragment 在 Leanback 上播放视频时如何将缩放模式设置为拉伸到全屏

问题描述

没有 ExoPlayer的 Google 的Leanback 展示视频播放器

带有 ExoPlayer的 Google Leanback 展示视频播放器

我已经试用了谷歌的leanback 展示,它也在“适合屏幕”模式下播放视频,两侧有黑条。我在 API 文档的任何地方都找不到更改缩放模式的选项

标签: androidandroid-tvleanback

解决方案


我必须调查VideoFragment源头才能弄清楚这一点。VideoFragment有一个简单SurfaceView的作为其布局的根元素,您所要做的就是使SurfaceView匹配父(即设备屏幕)的宽度和高度。为此,只需覆盖onVideoSizeChanged并使用getSurfaceView来获取SurfaceViewVideoFragment.

@Override
protected void onVideoSizeChanged(int width, int height) {
    switch (scaleMode) {
        //Flag indicates that this video should stretch to screen
        case MediaMetaData.SCALE_MODE_STRETCH:  
            View rootView = getView();
            SurfaceView surfaceView = getSurfaceView();
            ViewGroup.LayoutParams params = surfaceView.getLayoutParams();
            params.height = rootView.getHeight();
            params.width = rootView.getWidth();
            surfaceView.setLayoutParams(params);
            break;
        //When the video shouldn't stretch, just invoke super to have the VideoFragment's default behavior which is fit to screen
        default:                            
            super.onVideoSizeChanged(width, height);
    }
}

推荐阅读