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?
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"/>
From the documentation:
If you require fine-grained control over the player controls and the
Surfaceonto which video is rendered, you can set the player’s targetSurfaceView,TextureView,SurfaceHolderorSurfacedirectly using SimpleExoPlayer’ssetVideoSurfaceView,setVideoTextureView,setVideoSurfaceHolderandsetVideoSurfacemethods 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".
Note, TextureView can only be used in a hardware accelerated window. When rendered in software, TextureView will draw nothing. so after set surface type
<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"/>
you need to make sure that hardware acceleration is enabled, go to manifest file and ad this line
<application android:hardwareAccelerated="true" ...>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With