Skip to content

No way to create the "IAttributeSet" required by Android ExoPlayer constructor in .NET/C# #102300

Closed
@jonmdev

Description

@jonmdev

Description

ExoPlayer is the Android video player recommended by Google and used by the MAUI Community Toolkit for video playback.

It has the following constructor in .NET:

public unsafe StyledPlayerView (global::Android.Content.Context? context, global::Android.Util.IAttributeSet? attrs) : base (IntPtr.Zero, JniHandleOwnership.DoNotTransfer)

ExoPlayer can be constructed with a SurfaceView, TextureView, or none type of surface_type. This surface_type determines rendering behavior of the video player. It can only be declared on construction of the ExoPlayer.

In order to specify this, we are intended by Google to create or load some XML stating the surface_type, convert it to an IAttributeSet and then pass it to this constructor.

However, this does not work in .NET/C# and there is therefore currently no way to build a TextureView type ExoPlayer. SurfaceView is the default but TextureView is needed in many situations, and there is no way in .NET to get it.

Prior Discussion

See:

Despite two Android discussions, two MAUI discussions, and two StackOverflow discussions, no one anywhere can propose a working method for this to be done. Thus it seems this is a C#/.NET bug that must be fixed so we can use this properly.

By contrast a supposedly working Kotlin method is proposed here, but the functions referenced don't have matching C# equivalents so this is a dead end.

My Best Attempt

My best attempt to do this is is with adding the following code to a default .NET Android Application:

protected override void OnCreate(Bundle? savedInstanceState) {
    base.OnCreate(savedInstanceState);

    string xmlString =
        "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
        "<Com.Google.Android.Exoplayer2.UI.StyledPlayerView " +
        //"<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\"/>";

    XmlReader xmlReader = XmlReader.Create(new StringReader(xmlString));
    xmlReader.Read(); // https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreader.read?view=net-8.0
    
    Android.Util.IAttributeSet attributes = Android.Util.Xml.AsAttributeSet(xmlReader);
    
    //BUILD EXOPLAYER
    Com.Google.Android.Exoplayer2.UI.StyledPlayerView styledPlayerView = new(this, attributes);
    System.Diagnostics.Debug.WriteLine("SURFACE TYPE " + styledPlayerView.VideoSurfaceView.GetType());

Error

However, this returns the error:

**Java.Lang.ClassCastException:** 'android.util.XmlPullAttributes cannot be cast to android.content.res.XmlBlock$Parser'

The only reference I can find to this error suggests it is an insurmountable dead end: https://stackoverflow.com/questions/33331935/xmlpullattributes-cannot-be-cast-to-xmlblockparser#45918669

So what is the solution? Can .NET be fixed so that we can use this constructor and build ExoPlayer with a TextureView rather than SurfaceView?

Bug Project

See reproduction bug Project here: https://github.com/jonmdev/AndroidIAttributeSetBug

Reproduction Steps

Play bug project above. Or, to remake it, in Visual Studio 2022:

  1. File > New > Android Application
  2. Add the ExoPlayer NuGet Xam.Plugins.Android.ExoPlayer
  3. Then in MainActivity.cs, copy/paste the code from above or also here: https://github.com/jonmdev/AndroidIAttributeSetBug/blob/main/AndroidIAttributeSetBug/MainActivity.cs

Expected behavior

Should be able to make a TextureView ExoPlayer.

Actual behavior

Dead end error - cannot do so.

Regression?

I can find no reference anywhere on the Internet that suggests this ever worked or anyone has ever successfully made it work.

Known Workarounds

None known seemingly by anyone anywhere.

Other information

Any help toward a solution is greatly appreciated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions