react-native-track-player icon indicating copy to clipboard operation
react-native-track-player copied to clipboard

Audio focus issue on Android

Open jeffmon opened this issue 6 months ago • 1 comments

Describe the Bug On Android only: When audio is playing in our app (using react-native-track-player) and another app like Spotify is opened to play audio, Spotify starts playing for about half a second, then pauses. Meanwhile, our app’s audio continues playing uninterrupted.

Expected Behavior: When audio is playing in our app, and the user starts audio in Spotify (or any other app), our app’s audio should pause automatically, allowing the other app’s audio to play successfully.

Code To Reproduce

export const setupTrackPlayer = async () => {
  let capabilities = [
    Capability.Play,
    Capability.Pause,
    Capability.JumpForward,
    Capability.JumpBackward,
  ];
  await setupPlayer({
    minBuffer: 20 * 60,
    maxBuffer: 30 * 60,
    autoHandleInterruptions: true,
    androidAudioContentType: AndroidAudioContentType.Speech,
  });
  await TrackPlayer.updateOptions({
    android: {
      appKilledPlaybackBehavior: AppKilledPlaybackBehavior.StopPlaybackAndRemoveNotification,
    },
    stopIcon: STOP_BUTTON,
    capabilities: capabilities,
    compactCapabilities: capabilities,
    forwardJumpInterval: 30,
    backwardJumpInterval: 15,
    progressUpdateEventInterval: 1,
  });
};

We tried setting autoHandleInterruptions to false, and in that case, our app correctly paused when Spotify began playing audio. However, if Spotify was already playing and we started audio in our app, both audio streams would play at the same time.

We also attempted to use the android: { alwaysPauseOnInterruption: true } option, but it didn’t appear to have any effect.

Environment Info: Paste the results of npx react-native info:

System:
  OS: macOS 15.5
  CPU: (10) arm64 Apple M1 Pro
  Memory: 1.27 GB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 23.9.0
    path: /opt/homebrew/bin/node
  Yarn:
    version: 4.9.1
    path: /opt/homebrew/bin/yarn
  npm:
    version: 10.9.2
    path: /opt/homebrew/bin/npm
  Watchman:
    version: 2025.02.17.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.0
    path: /Users/*/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.1
      - iOS 18.1
      - macOS 15.1
      - tvOS 18.1
      - visionOS 2.1
      - watchOS 11.1
  Android SDK: Not Found
IDEs:
  Android Studio: 2024.3 AI-243.24978.46.2431.13208083
  Xcode:
    version: 16.1/16B40
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /Users/*/.sdkman/candidates/java/current/bin/javac
  Ruby:
    version: 2.7.5
    path: /Users/*/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli":
    installed: 15.0.1
    wanted: 18.0.0
  react:
    installed: 19.0.0
    wanted: 19.0.0
  react-native:
    installed: 0.78.2
    wanted: 0.79.2
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Paste the exact react-native-track-player version you are using: "react-native-track-player": "4.1.1"` Real device? Or simulator? Real Android devices and simulators What OS are you running? MacOS

jeffmon avatar Jun 06 '25 01:06 jeffmon

autohandleinterruptions: true is using andeoids native focus mangaer which should work. in this case ud need to use autohandleinterruptions in the example app to reproduce this behavior, bc chances are something in your app is causing this not the lib

lovegaoshi avatar Jun 06 '25 14:06 lovegaoshi

Closing due to lack of repro in the example app.

jspizziri avatar Jun 30 '25 12:06 jspizziri