react-native-track-player: [AVPlayerItem addOutput:] Cannot attach an output that is already attached.

Describe the Bug I am getting some kind of error on SwiftAudioEx.

[AVPlayerItem addOutput:] Cannot attach an output that is already attached.

Steps To Reproduce TBH it did not happen to me on debug, but it seems that is happening to my users in production. I have reports from Firebase Crashlytics.

Environment Info:

System:
    OS: macOS 13.0
    CPU: (8) arm64 Apple M1
    Memory: 137.73 MB / 16.00 GB
    Shell: 5.8.1 - /bin/zsh
  Binaries:
    Node: 18.11.0 - /usr/local/bin/node
    Yarn: 1.23.0-20220130.1630 - /usr/local/bin/yarn
    npm: 8.19.2 - /opt/homebrew/bin/npm
    Watchman: 4.9.0 - /usr/local/bin/watchman
  Managers:
    CocoaPods: 1.11.3 - /opt/homebrew/lib/ruby/gems/3.0.0/bin/pod
  SDKs:
    iOS SDK:
      Platforms: DriverKit 22.1, iOS 16.1, macOS 13.0, tvOS 16.1, watchOS 9.1
    Android SDK:
      API Levels: 23, 26, 27, 28, 29, 30, 31, 33
      Build Tools: 27.0.3, 28.0.3, 29.0.2, 29.0.3, 30.0.2, 30.0.3, 31.0.0
      System Images: android-28 | Google Play Intel x86 Atom, android-30 | Google APIs ARM 64 v8a, android-30 | Google Play ARM 64 v8a
      Android NDK: 21.0.6113669
  IDEs:
    Android Studio: Dolphin 2021.3.1 Patch 1 Dolphin 2021.3.1 Patch 1
    Xcode: 14.1/14B47b - /usr/bin/xcodebuild
  Languages:
    Java: 1.8.0_241 - /usr/bin/javac
  npmPackages:
    @react-native-community/cli: Not Found
    react: 18.0.0 => 18.0.0 
    react-native: 0.69.6 => 0.69.6 
    react-native-macos: Not Found
  npmGlobalPackages:
    *react-native*: Not Found

Using react-native-track-player@^3.2.0

This happens on real devices, most of them on iOS 15.

How can you help

I have seen a PR on SwiftAudioEx that might fix this: https://github.com/doublesymmetry/SwiftAudioEx/pull/30

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 47 (11 by maintainers)

Most upvoted comments

I am also seeing this crash in production – will spend some time to fix it in the coming days.

I’m getting this in production, with (what I think is) only a singleton TrackPlayer. Specifically:

  1. I call TrackPlayer.registerPlaybackService(() => PlaybackService) only in index.js at the root of my app, right after AppRegistry.registerComponent(appName, () => App), according to the docs.
  2. PlaybackService calls await setupPlayer as its first action, since the rest of the service requires TrackPlayer.

Can the playback service be called by iOS multiple times during the same app lifetime? Because if so, I can see how setupPlayer would be called multiple times (in that case).

But if so, what’s the right order in which to instantiate both the playback service (which almost definitely uses TrackPlayer) and to call setupPlayer? The docs say that setupPlayer needs to be called before any TrackPlayer functions; and yet it also says that you need to register the playback service immediately after AppRegistry.registerComponent. Should you end up with something like this in your index.js?

async function initAppAndPlayer() {
  await TrackPlayer.setupPlayer();

  AppRegistry.registerComponent(appName, () => App);

  // needs to happen right after registerComponent, but also after setupPlayer()
  TrackPlayer.registerPlaybackService(() => PlaybackService);
}

void initAppAndPlayer();

I’m on v4.0.1 and am getting this crash in production. Should we reactivate this issue so that it’s tracked, and close it once https://github.com/doublesymmetry/SwiftAudioEx/pull/74 gets into a release of RNTP?

Since I updated RNTP to 4.0.1 in my project I see increased amount of crashes:

Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0xec870 __exceptionPreprocess
1  libobjc.A.dylib                0x2bc00 objc_exception_throw
2  AVFCore                        0x87c74 -[AVPlayerItem(AVPlayerItemOutputs) addOutput:]
3  quinn                          0x43f298 partial apply for closure #1 in AVPlayerItemObserver.startObserving(item:) + 76 (AVPlayerItemObserver.swift:76)
4  quinn                          0x34ea0 thunk for @escaping @callee_guaranteed () -> () (<compiler-generated>)
5  libdispatch.dylib              0x4300 _dispatch_client_callout
6  libdispatch.dylib              0x77b8 _dispatch_continuation_pop
7  libdispatch.dylib              0x1b5c0 _dispatch_source_latch_and_call
8  libdispatch.dylib              0x1a190 _dispatch_source_invoke
9  libdispatch.dylib              0x128a8 _dispatch_main_queue_drain
10 libdispatch.dylib              0x125b0 _dispatch_main_queue_callback_4CF
11 CoreFoundation                 0x3720c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
12 CoreFoundation                 0x33f18 __CFRunLoopRun
13 CoreFoundation                 0x33668 CFRunLoopRunSpecific
14 GraphicsServices               0x35ec GSEventRunModal
15 UIKitCore                      0x22c2b4 -[UIApplication _run]
16 UIKitCore                      0x22b8f0 UIApplicationMain
17 quinn                          0x5000 main + 8 (main.m:8)
18 ???                            0x1c3ba6dcc (Missing)

Managed to reproduce in a unit test and made a fix in SwiftAudioEx. That’ll be merged in RNTP via #2176 and will go out with v4 of the library.

@fivecar setup the player as soon as possible