Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use TextureView programmatically with ExoPlayer?

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?

like image 560
user1695123 Avatar asked Aug 04 '26 02:08

user1695123


1 Answers

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)

like image 125
UnOrthodox Avatar answered Aug 06 '26 14:08

UnOrthodox