I create a PlayerView programmatically:
PlayerView playerView = new PlayerView(context);
Instead of using SurfaceView, I want it to use TextureView. In the documentation, it says to do the following:
<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"/>
However, like I said, I'm creating it programmatically, so how would I set it to use TextureView in my situation?
Step 1: Create an XML file in res/xml/player_view.xml
<androidx.media3.ui.PlayerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:surface_type="texture_view"
/>
Step 2: Create an AttributeSet, referencing the xml resource
val xmlAttributes = context.resources.getXml(R.xml.player_view).let {
try {
it.next()
it.nextTag()
} catch (e: Exception) {
// do something, log, whatever
}
Xml.asAttributeSet(it)
}
Step 3: Pass that into the constructor
PlayerView(context, xmlAttributes)
I'd like to note that this is stupid, you shouldn't have to do this, I hate that I had to, and I hate Exoplayer sometimes... (most times)
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