florent37 / Flutter-AssetsAudioPlayer

Play simultaneously music/audio from assets/network/file directly from Flutter, compatible with android / ios / web / macos, displays notifications
https://pub.dartlang.org/packages/assets_audio_player
Apache License 2.0
757 stars 363 forks source link

ForegroundServiceStartNotAllowedException #704

Open waplay opened 2 years ago

waplay commented 2 years ago

Flutter Version: 3.0.0 Lib Version: 3.0.4+3 Platform: Android

play(String url) async {
    try {
      await audioPlayer.open(
        Audio.liveStream(
          url,
        ),
        notificationSettings: const NotificationSettings(
          prevEnabled: false,
          nextEnabled: false,
          seekBarEnabled: false,
        ),
        showNotification: true,
        headPhoneStrategy: HeadPhoneStrategy.pauseOnUnplug,
        playInBackground: PlayInBackground.enabled,
        respectSilentMode: true,
      );
    } catch (e) {
      debugPrint('=== ERROR player: ' + e.toString());
    }
  }

Error message in Google Play Console:

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:3345)
  at android.os.Parcel.createExceptionOrNull (Parcel.java:2432)
  at android.os.Parcel.createException (Parcel.java:2421)
  at android.os.Parcel.readException (Parcel.java:2404)
  at android.os.Parcel.readException (Parcel.java:2346)
  at android.app.IActivityManager$Stub$Proxy.setServiceForeground (IActivityManager.java:8059)
  at android.app.Service.startForeground (Service.java:733)
  at com.github.florent37.assets_audio_player.notification.NotificationService.displayNotification (NotificationService.kt:306)
  at com.github.florent37.assets_audio_player.notification.NotificationService.access$displayNotification (NotificationService.kt:29)
  at com.github.florent37.assets_audio_player.notification.NotificationService$displayNotification$1.invokeSuspend (NotificationService.kt:154)
  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:938)
  at android.os.Handler.dispatchMessage (Handler.java:99)
  at android.os.Looper.loopOnce (Looper.java:226)
  at android.os.Looper.loop (Looper.java:313)
  at android.app.ActivityThread.main (ActivityThread.java:8663)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
Caused by: android.os.RemoteException: 
  at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked (ActiveServices.java:1955)
  at com.android.server.am.ActiveServices.setServiceForegroundLocked (ActiveServices.java:1455)
  at com.android.server.am.ActivityManagerService.setServiceForeground (ActivityManagerService.java:14124)
  at android.app.IActivityManager$Stub.onTransact (IActivityManager.java:3639)
  at com.android.server.am.ActivityManagerService.onTransact (ActivityManagerService.java:3034)

How to fix this problem?

nkek1027 commented 2 years ago

me too

Fatal Exception: android.app.ForegroundServiceStartNotAllowedException: Service.startForeground() not allowed due to mAllowStartForeground false: service com.appspot.nkek.babyhan.baby_asmr/com.github.florent37.assets_audio_player.notification.NotificationService
       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:3345)
       at android.os.Parcel.createExceptionOrNull(Parcel.java:2432)
       at android.os.Parcel.createException(Parcel.java:2421)
       at android.os.Parcel.readException(Parcel.java:2404)
       at android.os.Parcel.readException(Parcel.java:2346)
       at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:8091)
       at android.app.Service.startForeground(Service.java:733)
       at com.github.florent37.assets_audio_player.notification.NotificationService.displayNotification(NotificationService.java:482)
       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:81)
       at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(BaseContinuationImpl.java:13)
       at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.java:128)
       at android.os.Handler.handleCallback(Handler.java:938)
       at android.os.Handler.dispatchMessage(Handler.java:99)
       at android.os.Looper.loopOnce(Looper.java:226)
       at android.os.Looper.loop(Looper.java:313)
       at android.app.ActivityThread.main(ActivityThread.java:8669)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)
samatzp commented 1 year ago

Any solution?

samatzp commented 1 year ago

Any solution?

akarapetsas commented 1 year ago

Add in your manifest the following permissions:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />

Also add this service in your manifest too:

<service
        tools:node="replace"
        android:name=".notification.NotificationService"
        android:foregroundServiceType="mediaPlayback"
        android:enabled="true"
        android:exported="true">
</service>
zanozbot commented 1 year ago

@akarapetsas I added your proposed solution to my app's manifest, but still receive the same error.