XamarinCommunityToolkit
XamarinCommunityToolkit copied to clipboard
[Bug] MediaElement Crash - System.ArgumentException 'An item with the same key has already been added. Key: Xamarin.Forms.BindableProperty'
Description
The MediaElement on android occasionally crashes with a System.ArgumentException.
This seems to occur very randomly and rarely when you change the source of a MediaElement from the code behind.
I've only found this to occur on Android and not iOS.
Stack Trace
System.ArgumentException: 'An item with the same key has already been added. Key: Xamarin.Forms.BindableProperty'
[MediaPlayerNative] info/warning (3, 0) System.ArgumentException: 'An item with the same key has already been added. Key: Xamarin.Forms.BindableProperty'
[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: An item with the same key has already been added. Key: Xamarin.Forms.BindableProperty
[mono-rt] at System.Collections.Generic.Dictionary2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x0015a] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:572
[mono-rt] at System.Collections.Generic.Dictionary2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corefx/src/Common/src/CoreLib/System/Collections/Generic/Dictionary.cs:240
[mono-rt] at Xamarin.Forms.BindableObject.CreateAndAddContext (Xamarin.Forms.BindableProperty property) [0x00041] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:564
[mono-rt] at Xamarin.Forms.BindableObject.GetOrCreateContext (Xamarin.Forms.BindableProperty property) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:572
[mono-rt] at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x000d2] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:420
[mono-rt] at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:374
[mono-rt] at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in D:\a\1\s\Xamarin.Forms.Core\BindableObject.cs:349
[mono-rt] at Xamarin.CommunityToolkit.UI.Views.MediaElement.Xamarin.CommunityToolkit.UI.Views.IMediaElementController.set_Duration (System.Nullable`1[T] value) [0x00000] in <964de25b16794d918e4c00220cf2f406>:0
[mono-rt] at Xamarin.CommunityToolkit.UI.Views.MediaElementRenderer.MetadataRetrieved (System.Object sender, System.EventArgs e) [0x00022] in <964de25b16794d918e4c00220cf2f406>:0
[mono-rt] at (wrapper delegate-invoke) <Module>.invoke_void_object_EventArgs(object,System.EventArgs)
[mono-rt] at Xamarin.CommunityToolkit.UI.Views.FormsVideoView+<>c__DisplayClass4_0.<SetVideoPath>b__0 () [0x00031] in <964de25b16794d918e4c00220cf2f406>:0
[mono-rt] at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2476
[mono-rt] at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2020-02/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319
[mono-rt] --- End of stack trace from previous location where exception was thrown ---
[mono-rt]
[mono-rt] at Xamarin.CommunityToolkit.UI.Views.FormsVideoView.SetVideoPath (System.String path) [0x000b2] in <964de25b16794d918e4c00220cf2f406>:0
[mono-rt] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.65(intptr,intptr)
[mono-rt] at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.65(intptr,intptr)
[BpBinder] onLastStrongRef automatically unlinking death recipients:
Link to Reproduction Sample
Steps to Reproduce
- Change MediaElement Source in the code or viewModel.
- View the MediaElement
Expected Behavior
Changing the MediaElement's source displays the new video and runs as normal.
Actual Behavior
Changing the MediaElement's source causes app to crash.
Basic Information
- Version with issue: Toolkit 1.3.0 pre2 and Xamarin Forms 5.0.0.2125
- Platform Target Frameworks:
- iOS: N/A
- Android: 11
- Android Support Library Version: N/A
- Nuget Packages:
- Affected Devices: Pixel 4a 5g
Workaround
I've found no workaround for this issue.
I have the exact same issue, also happened randomly from time to time. After the app crashed, the video will play fine without any issue.
Xamarin Forms 5.0.0.2125 Xamarin CommunityToolkit 1.3.0-pre2 Android 10
Hi, did you find a solution ? I have the exact same issue. I created a custom MediaElement with a MediaSource.FromFile directly dowloaded in app ressources, but also happened from time to time. I observed that the problem happened maybe once every ten attempts. And it's happened more often on the first MediaElement initialization. But if I restart the app and open the exact same video, this issue never happen the following time. It is very anoying/frustrating... All the rest of my code is really clear, I spent a lot of time to detect why. No way to find a workaround process.
Xamarin Forms 5.0.0.2244 Xamarin CommunityToolkit 1.3.1 Android 11
@TryToNotWorkAround there really is no value added by using that language. It would have taken less effort to delete the statement than apologise for making it.
@bijington sorry you're right. It's okay, frustration is my own concern... Is this a recurring issue ? That's the only topic I found about it. I found another line of thinking through testing. If there is a delay between providing a media source in the code behind and initializing MediaElement, the application often crashes. Otherwise, there is a one in ten chance.
This issue is still occurring. Is there a prescribed fix for it? I tried delaying the loading of the control until I knew where the media will be located, but it's still randomly occurring.
Hi get this also.
Android: 12 Android Build: SKQ1.211006.001 Manufacturer: Xiaomi Model: M2101K9G CrashReporter Key: 1dfdb51d-bb1d-4af1-90ec-cc03c10f9877 Start Date: 2023-01-18T18:50:19.059Z Date: 2023-01-18T18:50:21.435Z
Xamarin Exception Stack:
System.ArgumentException: An item with the same key has already been added. Key: Xamarin.Forms.BindableProperty
at System.Collections.Generic.Dictionary2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x0015a] in <97c6c40360454c158c014ee803ea6785>:0 at System.Collections.Generic.Dictionary
2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in <97c6c40360454c158c014ee803ea6785>:0
at Xamarin.Forms.BindableObject.SetValueCore (Xamarin.Forms.BindableProperty property, System.Object value, Xamarin.Forms.Internals.SetValueFlags attributes, Xamarin.Forms.BindableObject+SetValuePrivateFlags privateAttributes) [0x000d2] in <1693287603d24707b07b71d38f4122f3>:0
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value, System.Boolean fromStyle, System.Boolean checkAccess) [0x0004d] in <1693287603d24707b07b71d38f4122f3>:0
at Xamarin.Forms.BindableObject.SetValue (Xamarin.Forms.BindableProperty property, System.Object value) [0x00000] in <1693287603d24707b07b71d38f4122f3>:0
at Xamarin.CommunityToolkit.UI.Views.MediaElement.Xamarin.CommunityToolkit.UI.Views.IMediaElementController.set_Duration (System.Nullable`1[T] value) [0x00000] in <0a68dce273b64a29949c8b1d7ab84eee>:0
at Xamarin.CommunityToolkit.UI.Views.MediaElementRenderer.MetadataRetrieved (System.Object sender, System.EventArgs e) [0x00022] in <0a68dce273b64a29949c8b1d7ab84eee>:0
at Xamarin.CommunityToolkit.UI.Views.FormsVideoView+<>c__DisplayClass4_0.<SetVideoPath>b__0 () [0x00031] in <0a68dce273b64a29949c8b1d7ab84eee>:0
at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in <97c6c40360454c158c014ee803ea6785>:0
at System.Threading.Tasks.Task.Execute () [0x00000] in <97c6c40360454c158c014ee803ea6785>:0
at null.null
at Xamarin.CommunityToolkit.UI.Views.FormsVideoView.SetVideoPath (System.String path) [0x000b2] in <0a68dce273b64a29949c8b1d7ab84eee>:0
at System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) [0x00000] in <97c6c40360454c158c014ee803ea6785>:0
at Android.App.SyncContext+<>c__DisplayClass2_0.<Post>b__0 () [0x00000] in <7685f94735ec4635bcf7c23732e3db0f>:0
at Java.Lang.Thread+RunnableImplementor.Run () [0x00008] in <7685f94735ec4635bcf7c23732e3db0f>:0
at Java.Lang.IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this) [0x00008] in <7685f94735ec4635bcf7c23732e3db0f>:0
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V (_JniMarshal_PP_V callback, System.IntPtr jnienv, System.IntPtr klazz) [0x00005] in <7685f94735ec4635bcf7c23732e3db0f>:0