Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash in constructor MediaSessionCompat (Android) #292

Closed
ragnarek81997 opened this issue Nov 4, 2017 · 20 comments
Closed

Crash in constructor MediaSessionCompat (Android) #292

ragnarek81997 opened this issue Nov 4, 2017 · 20 comments

Comments

@ragnarek81997
Copy link

I have a problem when creating an object of MediaSessionCompat.
Exception:
Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Temporarily solved this problem by calling in a ui thread (if so you can call it):

Android.App.Application.SynchronizationContext.Send(delegate
{
mediaSessionCompat = new MediaSessionCompat(applicationContext, "MusicStoreAudio", RemoteComponentName, pIntent);
}, null);

After this notice began to work, but not stable. Somewhat improved the situation of the call of the body of the method UpdatePlaybackState in the ui stream (again, this is not quite a normal method call in the ui thread):

public void UpdatePlaybackState(int state, int position = 0, string errorMessage = "")
        {
            Android.App.Application.SynchronizationContext.Send(delegate
            {
                if (CurrentSession == null && (_binder?.IsBinderAlive).GetValueOrDefault(false) && !string.IsNullOrWhiteSpace(_packageName))
                {
                    InitMediaSession(_packageName, _binder);
                }

                ...

                remoteControlClient?.SetTransportControlFlags(flags);
            }, null);
        }

What is the essence of the problem? how can i fix better?
Thanks.

@mike-rowley
Copy link
Contributor

It's hard to say what the problem is without more context to be able to reproduce the problem.

@ragnarek81997
Copy link
Author

The problem is that I'm not sure about the correctness of my decision.

@mike-rowley
Copy link
Contributor

I know nothing of the specifics of how you're getting the error, so I cannot comment on the decision you made. Context is critical.

@ragnarek81997
Copy link
Author

Is the reason for getting an exception is AplicationContext?

@mike-rowley
Copy link
Contributor

If you do not provide your app code in which you are "creating an object of MediaSessionCompat" I cannot help you. Send me a sample project reproducing the issue you are running into and I can try to help.

@ragnarek81997
Copy link
Author

https://github.com/ragnarek81997/MusicStoreMobile
https://github.com/ragnarek81997/MusicStoreMobile/blob/master/MediaManager.Android/MediaSession/MediaSessionManager.cs
line 86
Android.App.Application.SynchronizationContext.Send(delegate
{
mediaSessionCompat = new MediaSessionCompat(applicationContext, "MusicStoreAudio", RemoteComponentName, pIntent);
}, null);
replace to
mediaSessionCompat = new MediaSessionCompat(applicationContext, "MusicStoreAudio", RemoteComponentName, pIntent);

@mike-rowley
Copy link
Contributor

mike-rowley commented Nov 7, 2017

First off, set your Target Framework to 7.1, the 0.45 release code is compiled prior to the 8.0 SDK release, this library does not yet work with the 8.0 SDK. It is possible the problem you are seeing is related to the incompatible Android binaries.

Also I am unable to run the code in your Master branch. It starts a white screen then ends immediately on a Nexus 6p.

@ragnarek81997
Copy link
Author

sorry, I forgot that I tested the torrent library, because of this and ending immediately the application. already corrected you will see the logon page and the audio player notification (https://github.com/ragnarek81997/MusicStoreMobile/blob/master/MusicStoreMobile.Droid/Views/AudioPlayerView.cs)

@mike-rowley
Copy link
Contributor

Your Projects are still compiling with 8.0. Putting that aside, the player starts, plays the Aaron Neville song, not sure where your crash is?

@ragnarek81997
Copy link
Author

here https://github.com/ragnarek81997/MusicStoreMobile/blob/master/MediaManager.Android/MediaSession/MediaSessionManager.cs
delete Android.App.Application.SynchronizationContext.Send(delegate
in lines 84 and 146 (this is my fix)

@ragnarek81997
Copy link
Author

After that you will hear music, but notifications will not appear, this is a problem

@mike-rowley
Copy link
Contributor

I removed the MediaManager Projects and replaced them with the 0.45 Nuget package and music plays with no issues and the Notifications appear with no issues or crashes on a Nexus 6p with Oreo installed.

I don't know what else you may have changed in the MediaManager Projects that are causing the problem or when you pulled the code, there may be code in the branch that hasn't been fully regression tested prior to a Nuget release. However I am actively using the develop branch code with no issues.

If the Nuget package works, I have to assume that something has changed in your MediaManager Projects that is causing your issue. I would recommend using the Nuget package or going back to the 0.45 tag or at least using the most current commits in the develop branch.

@ragnarek81997
Copy link
Author

did you use monodroid 8.0?

@mike-rowley
Copy link
Contributor

I did, but wouldn't recommend it. You may run into other issues.

I used your exact code, unchanged except for adding the Nugets and removing the other projects.

@ragnarek81997
Copy link
Author

ragnarek81997 commented Nov 8, 2017

I did what you put in, but I do not see notifications, although music is playing.
Xiaomi Redmi Note 3 pro
Android 6.0.1

@mike-rowley
Copy link
Contributor

I have also verified backwards compatibility by testing this exact same solution on an Android 5.0 device, the music plays and the notification displays with no issue.

@ragnarek81997
Copy link
Author

fragment of output

11-08 01:22:51.085 I/mono-stdout( 1811): Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
11-08 01:22:51.091 I/mono-stdout( 1811):   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
11-08 01:22:51.092 I/mono-stdout( 1811):   at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00089] in <7cfbebb561c54efc9010b018c0846c7e>:0 
11-08 01:22:51.092 I/mono-stdout( 1811):   at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (System.IntPtr jobject, System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00015] in <e975227ac8644a30bb0866117325de0d>:0 
11-08 01:22:51.092 I/mono-stdout( 1811):   at Android.Runtime.JNIEnv.FinishCreateInstance (System.IntPtr instance, System.IntPtr jclass, System.IntPtr constructorId, Android.Runtime.JValue* constructorParameters) [0x00008] in <e975227ac8644a30bb0866117325de0d>:0 
11-08 01:22:51.092 I/mono-stdout( 1811):   at Android.Support.V4.Media.Session.MediaSessionCompat..ctor (Android.Content.Context context, System.String tag, Android.Content.ComponentName mbrComponent, Android.App.PendingIntent mbrIntent) [0x0010c] in <e7165be044e447a1b369a141df7a1f8b>:0 
11-08 01:22:51.093 I/mono-stdout( 1811):   at Plugin.MediaManager.MediaSession.MediaSessionManager.InitMediaSession (System.String packageName, Plugin.MediaManager.MediaServiceBinder binder) [0x00058] in <32e6264b15e94da186e88c6265ff1eae>:0 
11-08 01:22:51.093 I/mono-stdout( 1811):   --- End of managed Java.Lang.RuntimeException stack trace ---
11-08 01:22:51.093 I/mono-stdout( 1811): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () [0x0000c] in <896ad1d315ca4ba7b117efb8dacaedcf>:0 
  at Java.Interop.JniEnvironment+InstanceMethods.CallNonvirtualVoidMethod (Java.Interop.JniObjectReference instance, Java.Interop.JniObjectReference type, Java.Interop.JniMethodInfo method, Java.Interop.JniArgumentValue* args) [0x00089] in <7cfbebb561c54efc9010b018c0846c7e>:0 
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (System.IntPtr jobject, System.IntPtr jclass, System.IntPtr jmethod, Android.Runtime.JValue* parms) [0x00015] in <e975227ac8644a30bb0866117325de0d>:0 
  at Android.Runtime.JNIEnv.FinishCreateInstance (System.IntPtr instance, System.IntPtr jclass, System.IntPtr constructorId, Android.Runtime.JValue* constructorParameters) [0x00008] in <e975227ac8644a30bb0866117325de0d>:0 
  at Android.Support.V4.Media.Session.MediaSessionCompat..ctor (Android.Content.Context context, System.String tag, Android.Content.ComponentName mbrComponent, Android.App.PendingIntent mbrIntent) [0
x0010c] in <e7165be044e447a1b369a141df7a1f8b>:0 
  at Plugin.MediaManager.MediaSession.MediaSessionManager.InitMediaSession (System.String packageName, Plugin.MediaManager.MediaServiceBinder binder) [0x00058] in <32e6264b15e94da186e88c6265ff1eae>:0 
  --- End of managed Java.Lang.RuntimeException stack trace ---
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
	at android.os.Handler.<init>(Handler.java:203)
	at android.os.Handler.<init>(Handler.java:117)
	at android.support.v4.media.session.MediaSessionCompat.setCallback(MediaSessionCompat.java:457)
	at android.support.v4.media.session.MediaSessionCompat.setCallback(MediaSessionCompat.java:445)
11-08 01:22:51.093 I/mono-stdout( 1811): 	at android.os.Handler.<init>(Handler.java:203)
11-08 01:22:51.093 I/mono-stdout( 1811): 	at android.os.Handler.<init>(Handler.java:117)
11-08 01:22:51.094 I/mono-stdout( 1811): 	at android.support.v4.media.session.MediaSessionCompat.setCallback(MediaSessionCompat.java:457)
11-08 01:22:51.094 I/mono-stdout( 1811): 	at android.support.v4.media.session.MediaSessionCompat.setCallback(MediaSessionCompat.java:445)
11-08 01:22:51.094 I/mono-stdout( 1811): 	at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:410)
11-08 01:22:51.094 I/mono-stdout( 1811): 
	at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:410)
11-08 01:22:51.117 V/MediaPlayer( 1811): resetDrmState:  mDrmInfo=null mDrmProvisioningThread=null mPrepareDrmInProgress=false mActiveDrmScheme=false
11-08 01:22:51.117 V/MediaPlayer( 1811): cleanDrmObj: mDrmObj=null mDrmSessionId=null
11-08 01:22:51.128 W/MediaPlayer( 1811): Couldn't open https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3: java.io.FileNotFoundException: No content provider: https://ia800806.us.archive.org/15/items/Mp3Playlist_555/AaronNeville-CrazyLove.mp3
11-08 01:22:51.128 V/MediaHTTPService( 1811): MediaHTTPService(android.media.MediaHTTPService@b7a5bcf): Cookies: null
11-08 01:22:51.152 W/AudioManager( 1811): Use of stream types is deprecated for operations other than volume control
11-08 01:22:51.152 W/AudioManager( 1811): See the documentation of requestAudioFocus() for what to use instead with android.media.AudioAttributes to qualify your playback use case
11-08 01:22:51.176 V/MediaHTTPService( 1811): makeHTTPConnection: CookieManager(java.net.CookieManager@81d962b) exists.
11-08 01:22:51.176 V/MediaHTTPService( 1811): makeHTTPConnection(android.media.MediaHTTPService@b7a5bcf): cookieManager: java.net.CookieManager@81d962b Cookies: null
11-08 01:22:55.322 E/MediaMetadataRetrieverJNI( 1811): getEmbeddedPicture: Call to getEmbeddedPicture failed.
11-08 01:22:55.329 D/Mono    ( 1811): Assembly Ref addref Plugin.MediaManager[0xd98dc380] -> System[0xd8d7e3e0]: 13
11-08 01:22:56.559 D/Mono    ( 1811): DllImport searching in: '__Internal' ('(null)').
11-08 01:22:56.559 D/Mono    ( 1811): Searching for 'java_interop_jnienv_call_static_long_method_a'.
11-08 01:22:56.559 D/Mono    ( 1811): Probing 'java_interop_jnienv_call_static_long_method_a'.
11-08 01:22:56.559 D/Mono    ( 1811): Found as 'java_interop_jnienv_call_static_long_method_a'.
11-08 01:22:56.571 D/Mono    ( 1811): DllImport searching in: '__Internal' ('(null)').
11-08 01:22:56.571 D/Mono    ( 1811): Searching for 'java_interop_jnienv_call_boolean_method'.
11-08 01:22:56.571 D/Mono    ( 1811): Probing 'java_interop_jnienv_call_boolean_method'.
11-08 01:22:56.571 D/Mono    ( 1811): Found as 'java_interop_jnienv_call_boolean_method'.

@ragnarek81997
Copy link
Author

output on build droid project, maybe there is a problem

2>------ Rebuild All started: Project: MusicStoreMobile.Droid, Configuration: Debug Any CPU ------
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Animation.design_fab_in. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Animation.design_fab_out. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Color.abc_tint_switch_thumb. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Color.design_textinput_error_color_dark. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Color.design_textinput_error_color_light. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent_Info. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent_Line2. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent_Time. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent_Title. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.Widget_AppCompat_NotificationActionContainer. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.Widget_AppCompat_NotificationActionText. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Animation.design_fab_in. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Animation.design_fab_out. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Color.abc_tint_switch_thumb. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Color.design_textinput_error_color_dark. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Color.design_textinput_error_color_light. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent_Info. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent_Line2. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent_Time. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.TextAppearance_StatusBar_EventContent_Title. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.Widget_AppCompat_NotificationActionContainer. Please check that your Nuget Package versions are compatible.
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(1344,2): warning XA0106: Skipping MusicStoreMobile.Droid.Resource.Style.Widget_AppCompat_NotificationActionText. Please check that your Nuget Package versions are compatible.
2>  MusicStoreMobile.Droid -> E:\Projects\MusicStoreMobile\MusicStoreMobile.Droid\bin\Debug\MusicStoreMobile.Droid.dll
2>  No way to resolve conflict between "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e". Choosing "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" arbitrarily.
2>  No way to resolve conflict between "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" and "System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e". Choosing "System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" arbitrarily.

@mike-rowley
Copy link
Contributor

I sent you a PR to your repository with the code changes that work on my side.

Best of luck.

@ragnarek81997
Copy link
Author

Thank you very much for your help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants