本文介绍了全屏显示Exoplayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 RecyclerView ViewPager 中使用 exoplayer 播放演示视频(.mp4).我展示了具有自定义布局的视频控制器.到目前为止一切都很好.

I try to show the show video (.mp4) with exoplayer in RecyclerView and ViewPager. I show the video controller with custom layout. so far so good.

现在尝试像其他视频播放器一样全屏显示视频,以前如何使用,但在 exoplayer 文档中找不到好的方法.

Now try to fullscreen the video like other video player how use before but can't find a good way in the exoplayer doc.

有人可以帮助我吗?

推荐答案

ExoPlayer库当前不提供启用/禁用全屏模式的内置方法.恐怕您需要自己实现此功能或为此找到一些第三方代码.

The ExoPlayer library currently does not provide a built-in way to enable/disable fullscreen mode. You need to implement this yourself or find some third party code for this I'm afraid.

基本上需要两个步骤:

a)将窗口和活动属性设置为全屏和/或沉浸式模式,并(如果需要)更改为横向模式.那不难.请参见有关Android开发人员的页面.

a) Set the window and activity properties to fullscreen and/or immersive mode and (if desired) change to landscape mode. That's not difficult. See this page on Android Developers.

b)将渲染过渡到SimpleExoPlayerView(实际上是关于Surface的),以浸入模式覆盖整个视口.要在所有API级别上获得最佳用户体验,这都是更大的挑战.

b) Transition rendering to a SimpleExoPlayerView (actually it's about the Surface) which covers the entire viewport in immersive mode. This is more of a challenge to achieve optimal user experience on all API levels.

为了获得最佳的用户体验,我们希望在过渡到全屏和返回的同时保持播放器的播放状态,以继续无缝播放.在RecyclerView中,要解决所有API级别都比较棘手.

For an optimal user experience, we want to keep the player playing while doing the transition to full screen and back, to continue playback seamlessly. In a RecyclerView this is kind of tricky to solve for all API levels.

方法1

最简单的方法可能是在进入沉浸式模式后立即将一个SimpleExoPlayerView实例放置在布局顶部(某些人为此打开带有第二个视图的对话框,有些人仅将第二个视图放在顶部)在布局中以某种方式显示并按需显示/隐藏它.

The easiest way is probably having a separate instance of SimpleExoPlayerView which you put on top of your layout as soon as you entered immersive mode (some people open a dialog with the second view for this, some have the second view simply on top in the layout somehow and show/hide it on demand).

然后,您将播放器实例从RV中嵌入的SimpleExoPlayerView中分离出来,并通过调用静态帮助器方法:

Then you detach the player instance from the SimpleExoPlayerView embedded in the RV and attach it to the fullscreen view with a call to a static helper method:

SimpleExoPlayerView.switchTargetView(simpleExoPlayer, oldPlayerView, newPlayerView);

此方法在API> = 23上效果很好.使用API​​ 23的方法 MediaCodec.setOutputSurface 允许动态交换曲面已被添加.上面的静态方法确保应用了此技术.结果,音频和视频继续播放,并且进入和退出全屏状态的用户体验都非常流畅.对于API< = 22,需要创建一个新的编解码器实例以交换到另一个表面.这样会中断播放,并降低此方法的用户体验.

This approach works very well on API >=23. With API 23 the method MediaCodec.setOutputSurface allowing to swap surfaces dynamically has been added. The static method above makes sure this technique is applied. As a result audio and video keep playing and the user experience from entering and exiting fullscreen is super smooth. For API <=22 a new codec instance needs to be create to swap to another surface. This interrupts playback and the user experience for this approach is degraded.

方法2

为避免交换到较低API级别的另一个表面,您需要使用单个表面并将其转换为全屏模式.您可以只隐藏SimpleExoPlayerView以外的所有内容,并设置布局宽度和高度以使其与父级匹配,也可以将视频视图替换为占位符,然后放在顶部和背面.

To avoid swapping to another surface on lower API level, you need to use a single surface and transition it to fullscreen somehow. You can either just hide everything else than the SimpleExoPlayerView and set the layout width and height to match it's parent or, you can replace the video view with a placeholder and put it on top and back.

这对于简单的布局就可以很好地工作,但是对于复杂的布局(可能包括片段,viewpagers,recyclerviews),这可能是一个非常侵入性的操作,导致某些事情忽隐忽现或中断播放(例如,在某些API级别上,例如,从播放器视图中删除播放器视图时)视图层次结构).我已经看到这种方法在各种布局下都能很好地工作.

This can work quite well for simple layouts but with complex layouts probably including fragments, viewpagers, recyclerviews this can be a quite intrusive operation causing something to flicker or interrupting playback shortly (on some API levels for instance when removing the player view from the view hierarchy). I've seen this working nicely for various layouts though.

其他方法/挑战

当您深入研究和/或根本不使用SimpleExoPlayerView时,可能还有其他可能更好的方法.

There might be other and probably better approaches when you dig deeper and/or if you are not using SimpleExoPlayerView at all.

这篇关于全屏显示Exoplayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 05:38