Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 338
Elnur Hacıyev Avatar asked Mar 25 '18 13:03

Elnur Hacıyev


3 Answers

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"/>
like image 164
marcbaechinger Avatar answered Oct 06 '22 12:10

marcbaechinger


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".

like image 35
Kyle Venn Avatar answered Oct 06 '22 12:10

Kyle Venn


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

  • At application level: <application android:hardwareAccelerated="true" ...>
like image 41
Mostafa Anter Avatar answered Oct 06 '22 12:10

Mostafa Anter