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:
- File > New > Android Application
- Add the ExoPlayer NuGet
Xam.Plugins.Android.ExoPlayer
- 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:
xmlReader.AttributeCount
&attributes.AttributeCount
both return as0
which I presume means they are not getting any XML set into them correctly.- 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
After further research there is no way to do this in .NET except:
This does work. But there is no way to programmatically create the xml. It must be pre-stored in Resources and loaded from there.
Here is the working Kotlin code. I hope it will help you.
The differences I see:
xmlns:app
Xml:
Parse: