-
-
Notifications
You must be signed in to change notification settings - Fork 480
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
Adds androidStopForegroundOnCompleted to AudioServiceConfig which will stop foreground service when AudioProcessingState == AudioProcessingState.completed #1054
base: minor
Are you sure you want to change the base?
Conversation
…hich will stop foreground service when AudioProcessingState == AudioProcessingState.completed
Have you had a look at #932 (comment) > |
Hi Ryan, yes thanks. I have looked at all of the threads regarding this issue (I believe). We do not think the battery optimization workaround is the best approach for us though we considered it. For our application, it seems better to control when stopForeground being called when the audio is complete or stopped rather than changing the battery optimization level so we can start/stop the foreground service whenever we want. We do not really need to start/stop foreground service during playback, we just have audio process with multiple pauses though for the user, it is one continual audio stream. Hope this makes sense. |
It would also be possible to use the current implementation as is and just emit a |
Thanks, Ryan. Yes, I agree that would be another solution. I added the parameter option in this PR in case others do not want stopForeground when audio is completed. Thanks for taking the time to think through this. |
I may have misunderstood your response. In the current implementation emitting a !playing state does NOT trigger stopForeground. We would need adding that code to a non-playing state, such as the completed state. The PR implements it in the completed state with a parameter to turn that off/on. |
It should if you use the default config parameter stopForegroundOnPause. |
Right. That is what I was explaining above. Our audio pauses/unpauses throughout playback. We don't want to keep stopping foreground for each but only when it is complete. We run into exceptions for not having permissions to start foreground while in background. Could be just implementing stopForeground when state is completed. |
Yes, it would be nice if it turns out that it always makes sense to stopForeground on completed. The question is whether anyone has any use case where, for some reason, they don't want to stopForeground on completed, although it feels right that logically the pause state is similar to the completed state in this context. |
Hi @ryanheise, if you have a specific reservation about this PR, I'd be happy to address it. I believe these changes will help others who are running into this same issue where the app requires stopForeground on audio complete (not on pause). I'm open to making any necessary adjustments. |
Hey @skiluk, I was looking for a solution to this problem and came across your PR. I tried your patch on a phone with Android 8 and the result was positive: The media notification disappeared on |
Thank you for this. The ability to control the lifecycle of the audio service is a must, given that we can no longer resume the service without direct user input. I made a fork of this branch with a few minor modifications that I believe better align with best practices: https://github.com/Colton127/audio_service AudioProcessingState.idle: Stop foreground service and remove notification. It successfully removes the notification on Android 14 and Android 8, but not 11, which I think may not be possible. AudioProcessingState.completed: Stop foreground service, but keep notification active, allowing users to restart playback at a later time through the notification. I believe the lifecycle of the service should mirror that of Spotify and YouTube: The service remains active until the notification or app is swiped away by the user. |
This PR adds a AudioServiceConfig parameter androidStopForegroundOnCompleted which will stop foreground service when AudioProcessingState == AudioProcessingState.completed. I added this as a Feature Request as an app we are working on requires a solution like this.
We have an app that uses multiple audio files to play to the user in one stream of audio. Throughout this process we pause/unpause so the androidStopForegroundOnPause causes issues when playing in background as we do not have permissions to startForeground from the background. If we turn set androidStopForegroundOnPause to false, the notification is not hidden when the audio is completed or the user removes from the task list.
Using the feature added in this PR would allow the notification to be dismissed when the audio is completed.
See the feature request issue here:
#1053
Pre-launch Checklist
minor
branch OR my change is breaking and lands inmajor
branch.pubspec.yaml
according to the [pub versioning philosophy].* DESCRIPTION OF YOUR CHANGE (@your-git-username)
).///
).dart analyze
.dart format
.flutter test
and all tests are passing.