florent37/Flutter-AssetsAudioPlayer

Exception ocurring leading to crash: android.app.ForegroundServiceStartNotAllowedException

imyourdeveloperhere opened this issue ยท 12 comments

My Flutter Version: 3.7.0

Lib Version: 3.1.1

Platform Android

Platform : Android

Describe the bug

Occurring the Android 12 & 13 majorly, issue is the foreground.

com.github.florent37.assets_audio_player.notification.NotificationService.displayNotification

android.app.ForegroundServiceStartNotAllowedException

A clear and concise description of what the bug is.

Exception:

Exception android.app.ForegroundServiceStartNotAllowedException:
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:54)
at android.app.ForegroundServiceStartNotAllowedException$1.createFromParcel (ForegroundServiceStartNotAllowedException.java:50)
at android.os.Parcel.readParcelable (Parcel.java:3334)
at android.os.Parcel.createExceptionOrNull (Parcel.java:2421)
at android.os.Parcel.createException (Parcel.java:2410)
at android.os.Parcel.readException (Parcel.java:2393)
at android.os.Parcel.readException (Parcel.java:2335)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:7343)
at android.app.Service.startForeground (Service.java:733)
at com.github.florent37.assets_audio_player.notification.NotificationService.displayNotification (NotificationService.java)
at com.github.florent37.assets_audio_player.notification.NotificationService.access$displayNotification (NotificationService.java)
at com.github.florent37.assets_audio_player.notification.NotificationService$displayNotification$1.invokeSuspend (NotificationService.java)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith (BaseContinuationImpl.java)
at kotlinx.coroutines.DispatchedTask.run (DispatchedTask.java)
at android.os.Handler.handleCallback (Handler.java:938)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loopOnce (Looper.java:210)
at android.os.Looper.loop (Looper.java:299)
at android.app.ActivityThread.main (ActivityThread.java:8309)
at java.lang.reflect.Method.invoke
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:556)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1038)
Caused by android.os.RemoteException: Remote stack trace:
at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked (ActiveServices.java:1894)
at com.android.server.am.ActiveServices.setServiceForegroundLocked (ActiveServices.java:1403)
at com.android.server.am.ActivityManagerService.setServiceForeground (ActivityManagerService.java:12338)
at android.app.IActivityManager$Stub.onTransact (IActivityManager.java:3318)
at com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:2559)

I'm getting the same error. Is there any progress?

  • Flutter version: 3.10.6,
  • Assets Audio Player version : 3.1.1,
  • Only Android 14 (Api 34) issue,

FATAL EXCEPTION: main
Process: com.hayt****, PID: 12092
java.lang.SecurityException: Starting FGS with type mediaPlayback callerApp=ProcessRecord{c60496 12092:com.hay****/u0a192} targetSDK=34 requires permissions: all of the permissions allOf=true [android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK]
at android.os.Parcel.createExceptionOrNull(Parcel.java:3057)
at android.os.Parcel.createException(Parcel.java:3041)
at android.os.Parcel.readException(Parcel.java:3024)
at android.os.Parcel.readException(Parcel.java:2966)
at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:6761)
at android.app.Service.startForeground(Service.java:775)
at com.github.florent37.assets_audio_player.notification.NotificationService.displayNotification(NotificationService.kt:349)
at com.github.florent37.assets_audio_player.notification.NotificationService.access$displayNotification(NotificationService.kt:32)
at com.github.florent37.assets_audio_player.notification.NotificationService$displayNotification$1.invokeSuspend(NotificationService.kt:148)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at android.os.Handler.handleCallback(Handler.java:958)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:205)

For my application this method worked. I just added permission to the manifest. "android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK"
https://stackoverflow.com/a/76943772/16647487

Thanks to @omerb09 for pointing on a solution. I have made a PR which was already merged, so would suggest to try and use the plugin but point to that commit in master:

  assets_audio_player:
    git:
      url: https://github.com/florent37/Flutter-AssetsAudioPlayer.git
      ref: 91638830570e5e3c2212d04fd99b8d3b60380dd3

Thanks @vbuberen this version seems to work

Oh, didn't expect that fast feedback. In such case could you tell me if it works Ok for you on other Android versions?
Because I feel that this issue is connected with #803 but I only have a device with Android 14 where I can't reproduce #803 now even though I could in September. I am just curios if this change for Android 14 that I made with my PR somehow also affected the issue on Android 13 due to that missing permission.

I published my application with the version you mentioned for my test users. I didn't catch any errors with this issue. However, they threw out the error in #803. All errors occurred while the application was in the background. It also happened on Android 13 and 14.