media: Getting ForegroundServiceStartNotAllowedException

Media3 Version

1.0.0-beta01

Devices that reproduce the issue

Samsung Galaxy S10+ Samsung Galaxy S32 5G Samsung Galaxy A51 OPPO A93

Happened only on Android 12

Devices that do not reproduce the issue

Can’t reproduce manually, we got the stats from Crashlytics

Reproducible in the demo app?

Not tested

Reproduction steps

We extended our playback service by your MediaSessionService, and according to the crash stats in Crashlytics, the device states were 100% in the background, we actually instantiate the media controller right after the user starts to playback, but I’m not sure what’s going on under the hood.

Expected result

Since we don’t have control over starting the service as foreground, I’m wondering does it make sense to catch the exception on the library side, at least to avoid crashing in this case?

Actual result

Getting the crash below

Fatal Exception: android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.bandlab.bandlab/com.bandlab.media.player.notification.MediaPlaybackService
       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.startService(IActivityManager.java:6965)
       at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1926)
       at android.app.ContextImpl.startForegroundService(ContextImpl.java:1892)
       at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:796)
       at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:931)
       at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:703)
       at androidx.media3.session.MediaNotificationManager.updateNotificationInternal(MediaNotificationManager.java:203)
       at androidx.media3.session.MediaNotificationManager.updateNotification(MediaNotificationManager.java:179)
       at androidx.media3.session.MediaSessionService.onUpdateNotification(MediaSessionService.java:409)
       at androidx.media3.session.MediaNotificationManager$MediaControllerListener.onEvents(MediaNotificationManager.java:291)
       at androidx.media3.session.MediaControllerImplBase.lambda$new$0(MediaControllerImplBase.java:204)
       at androidx.media3.session.MediaControllerImplBase$$ExternalSyntheticLambda48.invoke(:4)
       at androidx.media3.common.util.ListenerSet$ListenerHolder.iterationFinished(ListenerSet.java:294)
       at androidx.media3.common.util.ListenerSet.handleMessage(ListenerSet.java:246)
       at androidx.media3.common.util.ListenerSet.$r8$lambda$bio3pd12v5B_9b5UeFaPn9XBQ90()
       at androidx.media3.common.util.ListenerSet$$ExternalSyntheticLambda0.handleMessage(:2)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       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)

Media

Nothing particular, just normal audio streaming

Bug Report

  • You will email the zip file produced by adb bugreport to dev.exoplayer@gmail.com after filing this issue.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 2
  • Comments: 21 (11 by maintainers)

Most upvoted comments

Summarizing the known causes for ForegroundServiceNotAllowedException and work arounds if available:

  • Pausing and resuming playback from an external device like with CastPlayer (see as reference issue #218). There isn’t a satisfactory way to work around this yet.

  • Retrying after a playback error when the app is in background crashes the app when the problem is fixed asynchronously. The reason for this is that when the playback error occurs, the service is taken off the foreground. Resuming playback then needs to be done from the foreground again. There isn’t a satisfactory way to work around this yet besides not retrying (or retry without async operations) in such a case. An app may want to post a notification when a playback error occurs to offer the user a way to resume playback by user interaction.

  • MediaButton Intents without exemption: Generally, only Bluetooth headphones are allowed to restart playback in the background with a media button Intent. This is true either when the app is running in the background and when the app has been terminated and playback resumption via a MediaButtonReceiver is attempted. The next release of the (legacy) androidx.media library, will have the MediaButtonReceiver updated to avoid crashes (see this change). Note that this is only required if an unauthorized app broadcast an media button event that is received by your receiver. The mentioned change aims to avoid the crash, but can’t make the use case work. Instead apps other than Bluetooth need to use a controller instead of broadcasting intents (aside: a MediaButtonReceiver for Media3 is planned for the 1.1.0 release).

  • Android 12: Voice commands can not resume playback. With the next release, such an exception will be caught by the Media3 library so that the app does not crash. The use case is still not working on Android 12 I’m afraid, but the app isn’t crashed. An app can register a MediaSessionService.Listener to be informed when such an exception is caught. An app can then for instance post a notification to inform the user and offer a notification to either go to the app or resume playback from the notification. This is implemented by the demo app of the main branch and will land in the next release that is to come soon.

That’s the state so far and all I know.

@kevinguitar & @chvp we know of a use-case where this error is being raised, when playback is started from other apps connected through MediaSession, i.e. playback did not start as a result of a notification posted by the library, specifically on Android 12. We’re working on this use-case and I’ll update the issue for any findings/solution/advice.

That’s clear, thanks. I did not know about the changes to the framework. I can confirm I can no longer repro this issue with my smartwatch on Android 13 (because this issue was not updated, I assumed this wasn’t fixed yet, so I didn’t try).

(This is also the only issue I am subscribed to, so I didn’t see any other “is this fixed yet” comments. I get that those can be annoying 🙂.)

Have you tried? If you are still seeing this, please expand your comment to make it actionable. Check my response to your comment in the other issue. We need the same details from you as I have explained there.

I can reliably reproduce this issue by starting playback with the media controls of my smartwatch. (Android 12, Oneplus 6T) Smartwatch is a Garmin Vivoactive 4.