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

Recover state on resuming application #63

Closed
Dekkee opened this issue May 21, 2019 · 13 comments
Closed

Recover state on resuming application #63

Dekkee opened this issue May 21, 2019 · 13 comments
Assignees

Comments

@Dekkee
Copy link

Dekkee commented May 21, 2019

Describe the bug
Recover playerState after minimize/maximize application

To Reproduce
Steps to reproduce the behavior:

  1. Start application
  2. Start playing
  3. Press power button to lock phone
  4. Press power button to turn on screen
  5. Pause playing from
  6. Unlock phone
  7. Go to application
  8. Application didn't switched state to "paused"

Expected behavior
Application in state "paused"

Runtime Environment (please complete the following information):

  • Samsung Galaxy S7

Flutter SDK version

[√] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
[√] Android Studio (version 3.4)
[√] IntelliJ IDEA Ultimate Edition (version 2019.1)
[√] VS Code, 64-bit edition (version 1.28.0)
[√] Connected device (1 available)

Additional context
As fast fix I can do it in my app by reading state in connect method. But maybe you have some solutions for this?

@ryanheise ryanheise self-assigned this May 21, 2019
@ryanheise
Copy link
Owner

Hi @Dekkee

Does this problem exhibit itself in the example app on your device?

@Dekkee
Copy link
Author

Dekkee commented May 21, 2019

Yes. In example from this repo. In both examples. Real and TTS.

@Dekkee
Copy link
Author

Dekkee commented May 21, 2019

On your device everything is fine?

@Dekkee
Copy link
Author

Dekkee commented May 21, 2019

In my case i solve it with this snippet in connect() method:

  void recoverState() {
    final media = AudioService.currentMediaItem;
    if (media != null) {
      _onMediaChanged(media);
    }
    final state = AudioService.playbackState;
    if (state != null) {
      _onStateChanged(state.basicState);
      _onProgressChanged(state.position);
    }
    debugPrint(
        'recoverState media: ${media?.title} state: ${state?.basicState}');
  }

@ryanheise
Copy link
Owner

Ah, you're right. In the example, the state changes are sent over the playbackStateStream immediately upon calling connect() but the client is not subscribed to the stream yet. So the solution is to switch the order around so that you subscribe to the stream(s) first and then call connect().

I've updated the example accordingly. Let me know if that also works for you.

@Dekkee
Copy link
Author

Dekkee commented May 22, 2019

Ok, I will try. But your idea with rxdart + BehaviorSubject is a good idea. It will resend last message on resubscribing.

@ryanheise
Copy link
Owner

Yes, I agree the rxdart approach proposed in my first comment (which is now unfortunately deleted) would be more robust. If I were to rewrite the example with a StreamBuilder I have a suspicion that it will subscribe too late and suffer the same consequences, so ultimately BehaviorSubject is probably the way to go. I'll test this tonight.

@Dekkee
Copy link
Author

Dekkee commented May 22, 2019

Current example works. Thank you!

@Dekkee Dekkee closed this as completed May 22, 2019
@ryanheise
Copy link
Owner

FYI, I've just changed the streams to use BehaviourSubject so it should be less sensitive to the order in which you connect vs subscribe, and I've changed the example app to use a StreamBuilder.

@Dekkee
Copy link
Author

Dekkee commented May 22, 2019

Nice! Thanks!

@Dekkee
Copy link
Author

Dekkee commented May 23, 2019

will you bump version?

@ryanheise
Copy link
Owner

Yep, just published it :-)

@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 23, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants