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:
Xam.Plugins.Android.ExoPlayer
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 as 0
which I presume means they are not getting any XML set into them correctly.**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.
After further research there is no way to do this in .NET except:
<?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" />
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.