skip to Main Content

I am trying to use an ExoPlayer constructor in Android .NET which requires an IAttributeSet to set some parameters as per this question.

I am trying to solve the creation of the IAttributeSet, but cannot seem to succeed.

In Visual Studio 2022, if one goes:

  1. File > New > Android Application
  2. Add the ExoPlayer NuGet Xam.Plugins.Android.ExoPlayer
  3. Then in MainActivity.cs, one can try the following code:
protected override void OnCreate(Bundle? savedInstanceState) {
    base.OnCreate(savedInstanceState);
    
    string xmlString =
        "<?xml version="1.0" encoding="utf-8"?>n" +
        "<Com.Google.Android.Exoplayer2.UI.PlayerView " +           
        "android:id="@+id/player_view" " +
        "app:surface_type="texture_view" " + //KEY NEEDED LINE FOR GOAL
        "android:layout_width="match_parent" " +
        "android:layout_height="match_parent"/>";
    
    XmlReader xmlReader = XmlReader.Create(new StringReader(xmlString));
    System.Diagnostics.Debug.WriteLine("XML READER " + xmlReader.AttributeCount);
            
    Android.Util.IAttributeSet attributes = Android.Util.Xml.AsAttributeSet(xmlReader);
    System.Diagnostics.Debug.WriteLine("ATTRIBUTES " + attributes.AttributeCount);
    
    Com.Google.Android.Exoplayer2.UI.StyledPlayerView styledPlayerView = new(this, attributes);            

No errors are given in Visual Studio on coding. However two problems are evident on running it:

  1. xmlReader.AttributeCount & attributes.AttributeCount both return as 0 which I presume means they are not getting any XML set into them correctly.
  2. The ExoPlayer constructor line returns the error **Java.Lang.ClassCastException:** 'android.util.XmlPullAttributes cannot be cast to android.content.res.XmlBlock$Parser'

Why is this code is failing on these two points? The only other place I see the error from (2) is here and I am not sure of how this works.

How can I create the IAttributeSet that is required for the ExoPlayer constructor?

Thanks for any help.

2

Answers


  1. Chosen as BEST ANSWER

    After further research there is no way to do this in .NET except:

    1. Create XML file named "test.xml" in Resourceslayout
    2. Copy paste into it:
    <?xml version="1.0" encoding="utf-8" ?> 
    <com.google.android.exoplayer2.ui.StyledPlayerView 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/test"
        app:surface_type="texture_view"
        android:layout_width= "match_parent"
        android:layout_height= "match_parent" />
    
    1. Run code in MainActivity.cs:
    XmlReader xmlResource = this.Resources.GetXml(Resource.Layout.test);
    xmlResource.Read();
    Android.Util.IAttributeSet attributes = Android.Util.Xml.AsAttributeSet(xmlResource);
    Com.Google.Android.Exoplayer2.UI.StyledPlayerView styledPlayerView = new(this, attributes); 
    

    This does work. But there is no way to programmatically create the xml. It must be pre-stored in Resources and loaded from there.


  2. Here is the working Kotlin code. I hope it will help you.
    The differences I see:

    1. Namespace in XML: xmlns:app
    2. Moving the parser to the first tag.

    Xml:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.media3.ui.PlayerView xmlns:app="http://schemas.android.com/apk/res-auto"
        app:surface_type="texture_view" />
    

    Parse:

    fun getPlayerViewAttributes(context: Context): AttributeSet {
        val parser = context.resources.getXml(R.xml.exoplayer)
        var type = 0
        while (type != XmlPullParser.END_DOCUMENT && type != XmlPullParser.START_TAG) {
            type = parser.next()
        }
        return Xml.asAttributeSet(parser)
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search