doublesymmetry / react-native-track-player

A fully fledged audio module created for music apps. Provides audio playback, external media controls, background mode and more!
https://rntp.dev/
Apache License 2.0
3.18k stars 980 forks source link

StopPlaybackAndRemoveNotification option is not worked 100% times #2328

Closed NinjaDevRN closed 1 week ago

NinjaDevRN commented 2 weeks ago

Describe the Bug The android does not respect AppKilledPlaybackBehavior.StopPlaybackAndRemoveNotification option The reproduction: 20/100. We have experienced the issue across different models of devices and different android system versions.

Our options:

await TrackPlayer.updateOptions({
    android: {
      appKilledPlaybackBehavior: AppKilledPlaybackBehavior.StopPlaybackAndRemoveNotification,
    },
    capabilities: [Capability.Play, Capability.Pause],
    notificationCapabilities: [Capability.Play, Capability.Pause],
    compactCapabilities: [Capability.Play, Capability.Pause],
    progressUpdateEventInterval: PROGRESS_UPDATE_EVENT_INTERVAL,
  });

Steps To Reproduce

  1. Open the app
  2. Leave the app opened for ~20 minutes
  3. Start playing track
  4. Minimize the app
  5. Close the app
  6. The track is still playing and the notification is presented in notification center
**Environment Info:** react-native-track-player versions: "4.0.1", "4.1.1". Real devices (developing, production) OS: Android System: OS: macOS 14.5 CPU: (10) arm64 Apple M1 Pro Memory: 93.53 MB / 16.00 GB Shell: version: "5.9" path: /bin/zsh Binaries: Node: version: 18.19.0 path: ~/.nvm/versions/node/v18.19.0/bin/node Yarn: version: 1.22.22 path: ~/.nvm/versions/node/v18.19.0/bin/yarn npm: version: 10.2.3 path: ~/.nvm/versions/node/v18.19.0/bin/npm Watchman: version: 2024.05.06.00 path: /opt/homebrew/bin/watchman Managers: CocoaPods: version: 1.14.3 path: /Users/ianiskondakovmercury/.rbenv/shims/pod SDKs: iOS SDK: Platforms: - DriverKit 23.5 - iOS 17.5 - macOS 14.5 - tvOS 17.5 - visionOS 1.2 - watchOS 10.5 Android SDK: API Levels: - "34" Build Tools: - 33.0.1 - 34.0.0 System Images: - android-34 | Google APIs ARM 64 v8a Android NDK: Not Found IDEs: Android Studio: 2023.3 AI-233.14808.21.2331.11926650 Xcode: version: 15.4/15F31d path: /usr/bin/xcodebuild Languages: Java: version: 17.0.11 path: /usr/bin/javac Ruby: version: 3.1.2 path: /Users/ianiskondakovmercury/.rbenv/shims/ruby npmPackages: "@react-native-community/cli": Not Found react: installed: 18.2.0 wanted: 18.2.0 react-native: installed: 0.73.8 wanted: 0.73.8 react-native-macos: Not Found npmGlobalPackages: "*react-native*": Not Found Android: hermesEnabled: true newArchEnabled: false iOS: hermesEnabled: true newArchEnabled: false

I found the conversation in Discord with the same issue. https://discord.com/channels/567636850513018880/1188151539764822036

But i can not find any solution.

lovegaoshi commented 2 weeks ago

this is due to the foregroundservice (MusicService) being stopped AND killed at that time and RNTP cannot restore the service and reattach properly as it should (eg. audio_service can). this disconnects the notificationManager and the service and/or the activity, causing turning off activity wont properly terminating the service.

u might get somewhere by fiddling with the service termination logic, or alternatively set stopForegroundGracePeriod to some astronomically large number to force keeping your service, at the expense of ~0.2% battery usage per hour.

NinjaDevRN commented 1 week ago

@lovegaoshi thank you for suggested workaround. Sorry for late response, we have tested the workaround.

I added stopForegroundGracePeriod: 99999999, and unfortunately it did not not help, and the reproduction now is 50/100.

Can you explain a bit more about this suggestion?

u might get somewhere by fiddling with the service termination logic

lovegaoshi commented 1 week ago

https://github.com/doublesymmetry/react-native-track-player/blob/9ff32f0f5a7b4f96900bf422810d587f5d2e3789/android/src/main/java/com/doublesymmetry/trackplayer/service/MusicService.kt#L774

u should also consult how audio_service archives this logic

On Thu, Jun 20, 2024, 1:01 AM NinjaDevRN @.***> wrote:

@lovegaoshi https://github.com/lovegaoshi thank you for suggested workaround. Sorry for late response, we have tested the workaround.

I added stopForegroundGracePeriod: 99999999, and unfortunately it did not not help, and the reproduction now is 50/100.

Can you explain a bit more about this suggestion?

u might get somewhere by fiddling with the service termination logic

— Reply to this email directly, view it on GitHub https://github.com/doublesymmetry/react-native-track-player/issues/2328#issuecomment-2180057792, or unsubscribe https://github.com/notifications/unsubscribe-auth/AZMOVVUE2HT5VZRRSH7DUZLZIKD45AVCNFSM6AAAAABJOQANAGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBQGA2TONZZGI . You are receiving this because you were mentioned.Message ID: @.*** com>

NinjaDevRN commented 1 week ago

Thank you!