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 981 forks source link

No audio coming from the car for CarPlay when playing audio tracks #2255

Closed watadarkstar closed 2 weeks ago

watadarkstar commented 4 months ago

Describe the Bug I'm currently playing audio using track player but the audio does not play via the car speakers. Audio comes from the phone instead.

Steps To Reproduce

  1. Play audio using track player
  2. Connect phone to car
  3. Notice how audio plays from device and not car yet the lock screen controls show up in Car Play

Code To Reproduce Please provide a simple code example that allows others to replicate the bug.

      await TrackPlayer.setupPlayer({
        // Important: We must set this to PlayAndRecord for Airplay to work.
        iosCategory: IOSCategory.PlayAndRecord,
        iosCategoryOptions: [IOSCategoryOptions.AllowAirPlay],
        iosCategoryMode: IOSCategoryMode.VideoRecording,
      })

I'm using mp3 files locally and via a url. Same issue.

Replicable on Example App? Can you replicate this bug in the React Native Track Player Example App?

TBD

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

info Fetching system and libraries information...
System:
  OS: macOS 13.3.1
  CPU: (8) x64 Apple M1
  Memory: 37.27 MB / 16.00 GB
  Shell:
    version: 3.2.2
    path: /usr/local/bin/fish
Binaries:
  Node:
    version: 18.17.1
    path: ~/.nvm/versions/node/v18.17.1/bin/node
  Yarn:
    version: 1.22.19
    path: ~/.nvm/versions/node/v18.17.1/bin/yarn
  npm:
    version: 9.6.7
    path: ~/.nvm/versions/node/v18.17.1/bin/npm
  Watchman:
    version: 2023.12.04.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.12.1
    path: /usr/local/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 22.4
      - iOS 16.4
      - macOS 13.3
      - tvOS 16.4
      - watchOS 9.4
  Android SDK:
    API Levels:
      - "29"
      - "30"
      - "33"
      - "34"
    Build Tools:
      - 29.0.2
      - 30.0.3
      - 33.0.0
      - 34.0.0
    System Images:
      - android-29 | Intel x86 Atom
      - android-29 | Intel x86 Atom_64
      - android-30 | Google APIs Intel x86 Atom
      - android-34 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2023.1 AI-231.9392.1.2311.11076708
  Xcode:
    version: 14.3.1/14E300c
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 11.0.21
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.72.10
    wanted: 0.72.10
  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.0.1",

Real device? Or simulator?

Real device. iOS iPhone 12 mini

What OS are you running?

Mac OSX

uzegonemad commented 4 months ago

FYI I do not have this issue. I'm not setting iosCategoryOptions and my scenes for CarPlay are set up roughly like this example: https://github.com/birkir/react-native-carplay/issues/132#issuecomment-1655803813 (I made a couple modifications to align with RN 0.73)

watadarkstar commented 4 months ago

@uzegonemad What are you setting for iosCategoryMode? I found that Carplay works for audio if I set that to IOSCategoryMode.SpokenAudio instead of IOSCategoryMode.VideoRecording

However, our app has both audio and video content which led me create this PR because I need to switch the iosCategoryMode at runtime: https://github.com/doublesymmetry/react-native-track-player/pull/2259

uzegonemad commented 4 months ago

@watadarkstar I'm not currently setting iosCategoryMode but I've got a commented line that was setting it to SpokenAudio. I believe podverse is doing something similar to modify the category mode as needed.

There are certain category modes that are incompatible with CarPlay by design, so it makes sense that VideoRecording wouldn't work but SpokenAudio would.

watadarkstar commented 3 months ago

For anyone who runs into this issue. To get Carplay working I had to do the following:

 await TrackPlayer.setupPlayer({
        // Important:  CarPlay works with IOSCategory.Playback.
        iosCategory: IOSCategory.Playback,
        // We must set this to the empty array, otherwise the player will
        // only play audio in airplane mode. AND it breaks lock screen controls if its not
        // the empty array.
        // https://react-native-track-player.js.org/docs/api/constants/ios-category-options#allowairplay
        iosCategoryOptions: [],
        // We must set this to SpokenAudio for lock screen controls to work with
        // IOSCategory.Playback.
        iosCategoryMode: IOSCategoryMode.SpokenAudio,
      })

The combination of these iosCategory settings are very important. Also, this library should probably have some documentation on how to get Carplay working.

Additionally, this library does not show the lock screen controls if you set:

iosCategory: IOSCategory.Playback,
iosCategoryOptions: [IOSCategoryOptions.AllowAirPlay],

Because IOSCategoryOptions.AllowAirPlay cannot be set if you set iosCategory to IOSCategory.Playback. See the Apple docs:

Setting this option enables the audio session to route audio output to AirPlay devices. You can only explicitly set this option if the audio session’s category is set to playAndRecord. For most other audio session categories, the system sets this option implicitly.

Found here: https://developer.apple.com/documentation/avfaudio/avaudiosession/categoryoptions/1771736-allowairplay

This library should probably throw a warning or error if you try to set IOSCategoryOptions.AllowAirPlay if you have:

iosCategory: IOSCategory.Playback,
github-actions[bot] commented 3 weeks ago

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 2 weeks ago

This issue was closed because it has been stalled for 7 days with no activity.