Skip to content
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

Way to discern between user killing task and requesting stop #344

Closed
yringler opened this issue Jun 5, 2020 · 12 comments
Closed

Way to discern between user killing task and requesting stop #344

yringler opened this issue Jun 5, 2020 · 12 comments
Assignees
Labels
3 testing enhancement New feature or request

Comments

@yringler
Copy link
Contributor

yringler commented Jun 5, 2020

Is your feature request related to a problem? Please describe.
I want to keep track of where a user is holding in an audio class. I can save the current location when user pauses, skips, changes to another class.
But if user kills the background service, for example by swiping away the notification if it's paused, I can't differentiate between whether the user clicked a stop button, which would mean resetting the position, or the notification was swiped away.

Describe the solution you'd like
A way to know when the audio service will be killed, not due to user clicking a stop button.
This could be another callback on BackgroundAudioTask or a static stream or future in AudioServiceBackground. There are probably other ways.

Describe alternatives you've considered
I considered updating the position every 5 seconds, so I'll never be that far off. But ti's not a clean solution, besides for not being accurate.

@yringler yringler added 1 backlog enhancement New feature or request labels Jun 5, 2020
@yringler
Copy link
Contributor Author

yringler commented Jun 5, 2020

I stand corrected - the stop callback is called when the task is killed.

@yringler yringler closed this as completed Jun 5, 2020
@yringler
Copy link
Contributor Author

yringler commented Jun 5, 2020

That isn't enough for my use case, because by convention if the user clicks a stop button, user expects position to be reset to start, but if user swipes away task, the expectation is that the current position will be saved.

Updated initial comment and issue title

@yringler yringler reopened this Jun 5, 2020
@yringler yringler changed the title way to run async code when audio service is killed Way to discern between user killing task and requesting stop Jun 5, 2020
@ryanheise
Copy link
Owner

Hi @yringler

I'm not sure if this is possible on iOS but on Android it could support it.

I think the way to go is to replace the current androidStopOnRemoveTask option with an onTaskRemoved callback that you override in the background audio task, which you can implement to do what you like. I could perhaps have the default implementation just call onStop. Would this work for you?

For iOS, you will still need to still use your current approach unfortunately.

@ryanheise
Copy link
Owner

Actually, the closest backward compatible default would be an empty implementation.

Then, instead of androidStopOnRemoveTask you could override as follows:

  void onTaskRemoved() {
    onStop();
  }

The androidStopForegroundOnPause isn't going away, but it would no longer influence the onTaskRemoved behaviour, so you'd need to override it as follows to emulate the old behaviour:

  void onTaskRemoved() {
    if (!AudioServiceBackground.state.playing) {
      onStop();
    }
  }

I'm not sure what would happen if the androidStopForegroundOnPause were on but the app didn't override this method accordingly. I guess after 10 minutes out of the foreground state, the OS may decide to destroy the service without any callbacks being called to gracefully clean up.

@ryanheise
Copy link
Owner

Sorry, what I was talking about above was handling swiping away the task in the task manager rather than swiping away the notification. Still, it may be useful to have callbacks for both of these.

Again, I'm not sure exactly how this would work on the iOS side...

@ryanheise
Copy link
Owner

I've just implemented the above in git master, and also added another callback:

void onClose() {
   onStop();
}

This will be called when the user swipes away the notification (on Android).

Unfortunately this is a breaking change, and you must update your manifest file to point to a different broadcast receiver:

        <receiver android:name="com.ryanheise.audioservice.MediaButtonReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.MEDIA_BUTTON" />
            </intent-filter>
        </receiver>

Let me know if it works for you, or if you uncover any bugs.

@yringler
Copy link
Contributor Author

yringler commented Jun 10, 2020

@ryanheise, it seems that play/pause buttons on android in notification drop down aren't working anymore?
-Possibly this is my bug - I'm looking into it now.-Yep, called it - I had forgotten to update AndroidManifest.xml.

@ryanheise
Copy link
Owner

Did you update your manifest?

@yringler
Copy link
Contributor Author

You are fast! Yes, I had realized after I posted that that I had forgotten that. Updated, just finished testing, works great!

@yringler
Copy link
Contributor Author

Thank you!
This is the last thing I wanted to add to my audio task with just_audio plugin. After I use it in production for a couple weeks, maybe later this month, I'll open a PR to include it in the README or something

@ryanheise
Copy link
Owner

Glad to hear!

I've recently been rewriting the README. It might be good to wait a couple of weeks as I'm still restructuring things, but hopefully I can make it simpler, briefer and easier to skim through for people who want to get going quickly.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs, or use StackOverflow if you need help with audio_service.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 19, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
3 testing enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants