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

Service stops when activity closes and it's foreground #834

Open
nt4f04uNd opened this issue Sep 29, 2021 · 7 comments
Open

Service stops when activity closes and it's foreground #834

nt4f04uNd opened this issue Sep 29, 2021 · 7 comments
Assignees
Labels
1 backlog bug Something isn't working

Comments

@nt4f04uNd
Copy link
Contributor

nt4f04uNd commented Sep 29, 2021

Which API doesn't behave as documented, and how does it misbehave?
If processing state was idle, then became ready and playing didn't change, something goes wrong, not sure what exactly, perhaps MediaSession should be reactivated.

Minimal reproduction project
https://github.com/nt4f04unds-archive/issues/tree/audio_service_834

To Reproduce (i.e. user steps, not code)

  1. Run the sample on Android 11
  2. Start the playback
  3. Switch media item
  4. Dismiss the activity
  5. The service stops while playing

Error messages

n/a

Expected behavior
Service shouldn't stop

Screenshots
n/a

Runtime Environment (please complete the following information if relevant):

  • Device: emulator
  • OS: Android 11

Flutter SDK version

[√] Flutter (Channel master, 2.6.0-12.0.pre.136, on Microsoft Windows [Version 10.0.19041.1237], locale ru-RU)
    • Flutter version 2.6.0-12.0.pre.136 at C:\dev\src\flutter
    • Upstream repository [email protected]:nt4f04und/flutter.git
    • FLUTTER_GIT_URL = [email protected]:nt4f04und/flutter.git
    • Framework revision aaa14b8004 (5 hours ago), 2021-09-29 06:03:04 -0400
    • Engine revision bccb3a57eb
    • Dart version 2.15.0 (build 2.15.0-156.0.dev)

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\danya\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.7.30621.155
    • Windows 10 SDK version 10.0.19041.0

[√] Android Studio (version 4.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] IntelliJ IDEA Community Edition (version 2020.3)
    • IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.3
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart

[√] VS Code (version 1.60.2)
    • VS Code at C:\Users\danya\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.26.0

[√] Connected device (5 available)
    • Redmi Note 5 (mobile)   • 40c9f14       • android-arm64  • Android 9 (API 28)
    • sdk gphone x86 (mobile) • emulator-5554 • android-x86    • Android 11 (API 30) (emulator)
    • Windows (desktop)       • windows       • windows-x64    • Microsoft Windows [Version 10.0.19041.1237]
    • Chrome (web)            • chrome        • web-javascript • Google Chrome 94.0.4606.61
    • Edge (web)              • edge          • web-javascript • Microsoft Edge 94.0.992.31

! Doctor found issues in 1 category.

Additional context
This causes other side effects, e.g. the service gets destroyed while the activity is reattaching to it, which prevents proper patching of #671

@nt4f04uNd nt4f04uNd added 1 backlog bug Something isn't working labels Sep 29, 2021
@nt4f04uNd
Copy link
Contributor Author

nt4f04uNd commented Oct 12, 2021

EDIT: edited wrong comment, just reproduced on Android 9

@nt4f04uNd
Copy link
Contributor Author

nt4f04uNd commented Oct 12, 2021

I'm wondering about which of playing and processingState should prevail.

  • Should the notification be created if playing true but processingState == idle? Currently this is yes
  • Should the service be stopped if playing true but processingState == idle, or should it wait until playing is false? Currently this is yes

I think it should either be no and yes, or yes and no.

@ryanheise
Copy link
Owner

Why not avoid passing in invalid states to the API?

@nt4f04uNd
Copy link
Contributor Author

nt4f04uNd commented Oct 12, 2021

Is it playing == true and processingState == idle? It would be still good to prevent notification when we are idle, right?

@ryanheise
Copy link
Owner

I'm just thinking about it from the perspective of the way the Android API's work where it is the app's responsibility to coordinate these two things.

Alternatively, maybe there is a clever way of making the plugin do something reasonable in this state, such as showing a notification without entering the foreground state.

@nt4f04uNd
Copy link
Contributor Author

Submitted a PR to fix the original issue. I think it sounds good to let processingState to manage the lifecycle of the service and playing and other parameters to update the look of the notification, because notification can exist without the service running.

Extracted this into #847

@ryanheise
Copy link
Owner

I've spent some time thinking about it, and while I originally thought I should just let the processingState and playing states determine these two aspects of the Android life cycle, I now would rather think about the way media apps typically work and try to interpret each of these state transitions in a more useful way. I've come up with this: #849

I think this would be less surprising to the average user and it tries to do something reasonable in weird states like idle && playing.

I haven't fully tested it, so feel free to see if it works properly on your own example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1 backlog bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants