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?
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"/>
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 targetSurfaceView
,TextureView
,SurfaceHolder
orSurface
directly using SimpleExoPlayer’ssetVideoSurfaceView
,setVideoTextureView
,setVideoSurfaceHolder
andsetVideoSurface
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"
.
-
Kyle Venn, thank you for your answer. I tried it, but if I use raw TextureView instead of SimpleExoPlayer, I can't benefit from ExoPlayer's advantages, such as playback controls, fixed aspect ratio etc. Should I code those features myself if I choose to use TextureView? Btw, I have to use TextureView, because user should be able to drag player view like on Youtube, and SurfaceView does not give smooth result below Nougat. Mar 25, 2018 at 18:14
-
You should be able to use
SimpleExoPlayer
withTextureView
like in my example. Do you meanSimpleExoPlayerView
? If you're using that, you can callsetPlayer()
on thePlayerView
and ensure that the player already has theTextureView
set on it. Does that make sense? Mar 25, 2018 at 18:21 -
1Actually, when reading the question, Kyle's answer is more accurate then mine. :) Not sure whether it would make sense to change the question to "with PlayerView of ExoPlayer" which apparently was the intended question. Thanks both. Mar 25, 2018 at 22:18
-
1Yes, this would be a more accurate title for the question. Changed it. Mar 26, 2018 at 14:43
-
1
PlayerView
defaults to using aSurfaceView
which, generally, shouldn't require a work around. So if it works, then you should be good to go just using theSurfaceView
. May 14, 2018 at 20:35
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" ...>