android / uamp

A sample audio app for Android
Apache License 2.0
13.11k stars 3.75k forks source link

UAMP crashes with ForegroundServiceStartNotAllowedException when targeting API 31/32 #485

Open DiegoPinedoEscribano opened 2 years ago

DiegoPinedoEscribano commented 2 years ago

The UAMP app crashes when trying to resume the playback session using the Google Assistant if the target and compile sdk is 31 or above

Steps to reproduce it:

    android.app.ForegroundServiceStartNotAllowedException: startForegroundService() not allowed due to mAllowStartForeground false: service com.example.android.uamp.next/com.example.android.uamp.media.MusicService
        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:3333)
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2420)
        at android.os.Parcel.createException(Parcel.java:2409)
        at android.os.Parcel.readException(Parcel.java:2392)
        at android.os.Parcel.readException(Parcel.java:2334)
        at android.app.IActivityManager$Stub$Proxy.startService(IActivityManager.java:6028)
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1855)
        at android.app.ContextImpl.startForegroundService(ContextImpl.java:1831)
        at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:779)
        at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:933)
        at androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:701)
        at com.example.android.uamp.media.MusicService$PlayerNotificationListener.onNotificationPosted(MusicService.kt:577)
        at com.google.android.exoplayer2.ui.PlayerNotificationManager.startOrUpdateNotification(PlayerNotificationManager.java:1164)
        at com.google.android.exoplayer2.ui.PlayerNotificationManager.handleMessage(PlayerNotificationManager.java:1409)
        at com.google.android.exoplayer2.ui.PlayerNotificationManager.lambda$ZG0s-7uOzb-YtyUlp8ahx3j4wBg(Unknown Source:0)
        at com.google.android.exoplayer2.ui.-$$Lambda$PlayerNotificationManager$ZG0s-7uOzb-YtyUlp8ahx3j4wBg.handleMessage(Unknown Source:2)

Does the UAMP project still needs to be updated to comply with the latest restrictions around Foreground Services introduced in API 31 ? If this is the case, do you have any idea on when this would happen?

Is it possible that the problem is within the Google Assistant app?

stezo commented 2 years ago

It's a shame that this wasn't addressed one year ago when Android 12 was introduced. Is Google really unaware of this or are they just ignoring the consequences their bad API decisions have on apps? I really want to know how to solve this problem. I mean, they force developers to target the newest API levels and don't provide help on how to address such problems.

And btw, I think this doesn't relate to the Google Assistant because I have seen such crashes on devices where Google services are not available at all. I don't know how to reproduce it locally, but it's a fact that this crash happens on production apps.

DiegoPinedoEscribano commented 2 years ago

It's a shame that this wasn't addressed one year ago when Android 12 was introduced. Is Google really unaware of this or are they just ignoring the consequences their bad API decisions have on apps? I really want to know how to solve this problem. I mean, they force developers to target the newest API levels and don't provide help on how to address such problems.

And btw, I think this doesn't relate to the Google Assistant because I have seen such crashes on devices where Google services are not available at all. I don't know how to reproduce it locally, but it's a fact that this crash happens on production apps.

We have opened an issue on their Issue Tracker in case you wanna also follow that one: https://issuetracker.google.com/u/1/issues/235172948

keithburgoyne commented 2 years ago

Also having this issue on one of our apps. I came here to see how Google solves the problem, only to find ... they haven't.

ChetanPatelSS commented 2 years ago

Any updates on this? We're also waiting for a solution

GauvainSeigneur commented 2 years ago

I think the issue come from this line in Music Service :

        if (!playWhenReady) {
                            // If playback is paused we remove the foreground state which allows the
                            // notification to be dismissed. An alternative would be to provide a
                            // "close" button in the notification which stops playback and clears
                            // the notification.
                            stopForeground(false)
                            isForegroundService = false
                        }

The problem is that notification can't be dismissed since Android 10 with a foreground service, so the team disable the foreground when the media is paused. But as you explained you try to resume with the app is in background and so the service tries to be in foreground again, and the ForegroundServiceStartNotAllowedException happens. A hotfix could be to provide the "close button" and remove this part of the code. Of course, when the notification is dismissed you have to stop/destroy the service. Also the boolean isForegroundService is not really a good solution, Android should provide a listener for the state of the service.

The safest way is to ask the service to be in foreground not inside the service but in an activity/fragment inside your app. If your app is in background you could use AlarmManager to wake up your phone and go to your application.

mnk98 commented 2 years ago

Any Update

guywest commented 1 year ago

Hello, Please post an update on this issue. Thank you