How to use TextureView instead of SurfaceView with PlayerView of ExoPlayer?

核能气质少年 提交于 2019-12-10 09:44:09

问题


I know it is possible to use TextureView in ExoPlayer. But I cannot find any sample on how to implement this functionality in a proper way. Could you please help me on this issue?


回答1:


The PlayerView has an xml attribute surface_type which let's you choose whether you want to use a SurfaceView or a TextureView.

(Note: SimpleExoPlayerView has been renamed to PlayerView in recent versions since it only depends on the Player interface and not on SimpelExoPlayerView anymore.)

You can choose texture_view, surface_view (default) or none. See the main section of the JavaDoc of PlayerView for details.

<com.google.android.exoplayer2.ui.PlayerView android:id="@+id/player_view"
     app:surface_type="texture_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"/>



回答2:


From the documentation:

If you require fine-grained control over the player controls and the Surface onto which video is rendered, you can set the player’s target SurfaceView, TextureView, SurfaceHolder or Surface directly using SimpleExoPlayer’s setVideoSurfaceView, setVideoTextureView, setVideoSurfaceHolder and setVideoSurface methods respectively.

So the relevant part here would be the setVideoTextureView(). Something like:

SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
TexturView textureView = findViewById(texture_view);
player.setVideoTextureView(textureView);

You can also create a Surface from a SurfaceTexture which you can get through TextureView's setSurfaceTextureViewListener(). See TextureView's documentation and this post. And then you could call setSurface(...) on the player.

But you shouldn't really need to do this anymore now that SimpleExoPlayer has the TextureView setter (we didn't always have this in old ExoPlayer). Also as another reference, here's the issue on ExoPlayer about supporting this.

As a side note, you can see the differences between SurfaceView and TextureView here. The general recommendation is to use a SurfaceView, but there are nuances.

[EDIT]

If you want to do it on the fly, you can instantiate the SimpleExoPlayer (not SimpleExoPlayerView) like I've written above, and then call setPlayer(...) on your PlayerView (which you could have defined in XML or dynamically).

If you don't need to set the surface/texture view on the fly, then you can just set it on the PlayerView in the layout XML via app:surface_type="texture_view".



来源:https://stackoverflow.com/questions/49476176/how-to-use-textureview-instead-of-surfaceview-with-playerview-of-exoplayer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!