expo: The AVPlayerItem instance has failed with the error code -11819 and domain "AVFoundationErrorDomain".

Summary

Our app downloads various audio files and writes them to the local File cache. Then it preloads them using await sound.loadAsync({ uri: localPath }), and after a while plays them, according to user interaction.

This works great in most cases. However, in about 1 of 50 preload operations, we get the following behavior:

  • The UI thread is locked / busy for 10 seconds. All animations, interactions, UI updates on the screen are paused. Animations from react-native-reanimated are stopped too.
  • During these 10 seconds, nothing crosses the bridge. Sound that is started before these 10 seconds is played to the end, Javascript tasks continue to run, but the JS side does not get any events from the native side, and it is not able to play new audio (or do anything else that involves the native side).
  • After these 10 seconds, the UI thread is back to normal, the animations resume (meaning they jump forward 10 seconds and then continue). Sound playback does not work anymore until you restart the app.

Around the end of those 10 seconds, Sentry records the following error:

code | EXAV
message | The AVPlayerItem instance has failed with the error code -11819 and domain "AVFoundationErrorDomain".
domain | RCTErrorDomain
userInfo | [null]
nativeStackIOS | 00 MyApp 0x00000001008490c0 _ZN8facebook5react11JSIExecutor21defaultTimeoutInvokerERKNSt3__18functionIFvvEEENS3_IFNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEvEEE + 70237211 MyApp 0x000000010081a2c8 _ZN8facebook5react11JSIExecutor21defaultTimeoutInvokerERKNSt3__18functionIFvvEEENS3_IFNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEvEEE + 51038022 MyApp 0x00000001009b5104 __cxa_throw + 41066833 MyApp  0x00000001009bc280 __cxa_throw + 43972044 libdispatch.dylib 0x00000001e5362e6c 355ACCF4-3917-3730-BC55-EF7003887ABE + 778855 libdispatch.dylib 0x00000001e5364a30 355ACCF4-3917-3730-BC55-EF7003887ABE + 1489666 libdispatch.dylib 0x00000001e5372f48 355ACCF4-3917-3730-BC55-EF7003887ABE + 7354477 libdispatch.dylib 0x00000001e5372b98 _dispatch_main_queue_callback_4CF + 4488 CoreFoundation 0x00000001e56b5800 5198FB57-5645-3B34-A49F-F32B52256CF3 + 33382499 CoreFoundation 0x00000001e566f704 5198FB57-5645-3B34-A49F-F32B52256CF3 + 468521010 CoreFoundation 0x00000001e5682bc8 CFRunLoopRunSpecific + 6001111 GraphicsServices 0x00000002017b6374 GSEventRunModal + 1641212 UIKitCore 0x00000001e7ff2648 3ED35565-456D-33CB-B554-6C567FA81585 + 53264081313 UIKitCore 0x00000001e7d73d90 UIApplicationMain + 3641414 MyApp 0x00000001006853a8 MyApp + 214161515 dyld 0x0000000100f8dce4 start + 520 | 0 | 0 MyApp 0x00000001008490c0 _ZN8facebook5react11JSIExecutor21defaultTimeoutInvokerERKNSt3__18functionIFvvEEENS3_IFNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEvEEE + 702372 | 1 | 1 MyApp 0x000000010081a2c8 _ZN8facebook5react11JSIExecutor21defaultTimeoutInvokerERKNSt3__18functionIFvvEEENS3_IFNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEvEEE + 510380 | 2 | 2 MyApp 0x00000001009b5104 __cxa_throw + 410668 | 3 | 3 MyApp 0x00000001009bc280 __cxa_throw + 439720 | 4 | 4 libdispatch.dylib 0x00000001e5362e6c 355ACCF4-3917-3730-BC55-EF7003887ABE + 7788 | 5 | 5 libdispatch.dylib 0x00000001e5364a30 355ACCF4-3917-3730-BC55-EF7003887ABE + 14896 | 6 | 6 libdispatch.dylib 0x00000001e5372f48 355ACCF4-3917-3730-BC55-EF7003887ABE + 73544 | 7 | 7 libdispatch.dylib 0x00000001e5372b98 _dispatch_main_queue_callback_4CF + 44 | 8 | 8 CoreFoundation 0x00000001e56b5800 5198FB57-5645-3B34-A49F-F32B52256CF3 + 333824 | 9 | 9 CoreFoundation 0x00000001e566f704 5198FB57-5645-3B34-A49F-F32B52256CF3 + 46852 | 10 | 10 CoreFoundation 0x00000001e5682bc8 CFRunLoopRunSpecific + 600 | 11 | 11 GraphicsServices 0x00000002017b6374 GSEventRunModal + 164 | 12 | 12 UIKitCore 0x00000001e7ff2648 3ED35565-456D-33CB-B554-6C567FA81585 + 5326408 | 13 | 13 UIKitCore 0x00000001e7d73d90 UIApplicationMain + 364 | 14 | 14 MyApp 0x00000001006853a8 MyApp + 21416 | 15 | 15 dyld 0x0000000100f8dce4 start + 520
             0 | 0 MyApp 0x00000001008490c0 _ZN8facebook5react11JSIExecutor21defaultTimeoutInvokerERKNSt3__18functionIFvvEEENS3_IFNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEvEEE + 702372
             1 | 1 MyApp 0x000000010081a2c8 _ZN8facebook5react11JSIExecutor21defaultTimeoutInvokerERKNSt3__18functionIFvvEEENS3_IFNS2_12basic_stringIcNS2_11char_traitsIcEENS2_9allocatorIcEEEEvEEE + 510380
             2 | 2 MyApp  0x00000001009b5104 __cxa_throw + 410668
             3 | 3 MyApp 0x00000001009bc280 __cxa_throw + 439720
             4 | 4 libdispatch.dylib 0x00000001e5362e6c 355ACCF4-3917-3730-BC55-EF7003887ABE + 7788
             5 | 5 libdispatch.dylib 0x00000001e5364a30 355ACCF4-3917-3730-BC55-EF7003887ABE + 14896
             6 | 6 libdispatch.dylib 0x00000001e5372f48 355ACCF4-3917-3730-BC55-EF7003887ABE + 73544
             7 | 7 libdispatch.dylib 0x00000001e5372b98 _dispatch_main_queue_callback_4CF + 44
             8 | 8 CoreFoundation 0x00000001e56b5800 5198FB57-5645-3B34-A49F-F32B52256CF3 + 333824
             9 | 9 CoreFoundation 0x00000001e566f704 5198FB57-5645-3B34-A49F-F32B52256CF3 + 46852
            10 | 10 CoreFoundation 0x00000001e5682bc8 CFRunLoopRunSpecific + 600
            11 | 11 GraphicsServices 0x00000002017b6374 GSEventRunModal + 164
            12 | 12 UIKitCore 0x00000001e7ff2648 3ED35565-456D-33CB-B554-6C567FA81585 + 5326408
            13 | 13 UIKitCore 0x00000001e7d73d90 UIApplicationMain + 364
            14 | 14 MyApp 0x00000001006853a8 MyApp + 21416
            15 | 15 dyld 0x0000000100f8dce4 start + 520

Managed or bare workflow? If you have ios/ or android/ directories in your project, the answer is bare!

managed

What platform(s) does this occur on?

iOS

SDK Version (managed workflow only)

45

Environment

expo-env-info 1.0.4 environment info: System: OS: macOS 12.3.1 Shell: 5.8 - /bin/zsh Binaries: Node: 16.15.1 - /opt/homebrew/opt/nvm/versions/node/v16.15.1/bin/node Yarn: 1.22.18 - /opt/homebrew/bin/yarn npm: 8.11.0 - /opt/homebrew/opt/nvm/versions/node/v16.15.1/bin/npm Watchman: 2022.03.21.00 - /opt/homebrew/bin/watchman Managers: CocoaPods: 1.11.3 - /opt/homebrew/bin/pod SDKs: iOS SDK: Platforms: DriverKit 21.4, iOS 15.5, macOS 12.3, tvOS 15.4, watchOS 8.5 IDEs: Xcode: 13.4.1/13F100 - /usr/bin/xcodebuild npmPackages: expo: ^45.0.0 => 45.0.4 react: 17.0.2 => 17.0.2 react-dom: 17.0.2 => 17.0.2 react-native: 0.68.2 => 0.68.2 Expo Workflow: managed

Reproducible demo

https://snack.expo.dev/@yolpsoftware/expo-av-crash

This Snack downloads some audio file to the local file system and then preloads and plays those audio files in fast succession. To show the UI thread’s usage, we use a react-native-reanimated animation that runs permanently.

After some seconds (usually 5-10 seconds, sometimes up to 1-2 minutes), the animation is freezed for exactly 10 seconds. During that time, the audio playback continues, but only of those audio files that have already started playback. No new audio files are played.

At the end of the 10 seconds, the animation resumes. In most cases, audio playback does not resume, even though the application tries to play more files. In some cases, Audio playback continues. We don’t know the cause for this seemingly random behavior.

Please note:

  • The problem only happens on an actual device. We have never seen the problem in a simulator.
  • The fast playback of many audio files might not be a normal use case. It is just to make sure we don’t have to wait a long time for the bug to happen. This bug is a big problem in our app, which has a much slower (normal) playback behavior. It just happens much less frequently. To demonstrate it, we created this snack.
  • We observed this bug the first time in May 2022, shortly after we migrated to Expo SDK 45 (previously 44). It may well be the case that this bug has been introduced in Expo 45.
  • The error “The AVPlayerItem instance …” (see stacktrace above) is probably not the actual error. To us, it seems that “The AVPlayerItem instance …” is a followup error that happens when you try to play audio after the audio system has stopped working. We do not have a stacktrace or an error message of the actual error.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

iOS can only decode up to 15 files max at the same time (no matter if video or audio). Android has been 8 or 10. It might have been 12. Are you sure that you never hit those numbers? The error you are reporting happened only to me when:

a) I created more than 15 AV instances and did not call unloadAsync / un-mounting those views b) I used a remote file on a server which did not support HTTP Byte Ranges or returned malformed headers

I had issues like that when I worked with videos and audio in a FlatList. The fix was to reduce windowSize and use viewableItemsChanges to make sure I unload my videos when I hit like 4 (to have some more room when navigating to another screen or opening modals with more videos). You need to make sure that you never exceed the system limits. iOS error numbers for AV are just trash and hard to debug.

Edit: after reviewing your code, the issue is clear. You’re exceeding the system limits. You create way too much sessions with your preload attempt and you do exceed what the system (decoder) can handle.

@hayata-suenaga are you sure these statements are actually ever true?

    if (playbackStatus.isLoaded) {
      if (playbackStatus.didJustFinish) {
        setIsPlaying(false);
        // Unload the audio from memory after it finishes playing
        await sound.unloadAsync();
        setSound(undefined);
      }
    }

Can both those statements (playbackStatus.isLoaded and playbackStatus.didJustFinish) be true at the same time?

If I were you I would make 100% sure that the audio files are really unloaded. Maybe add some console log and make sure those parts of the code that unload them are triggered properly.

I have dealt with many audio files in the same app, and I have only ever gotten the AVFoundationErrorDomain bug when not unloading audio files properly.

I had the exact same issue as @yolpsoftware and could not fix it either until I saw @hirbod post. I have added unloadAsync to whenever a sound finishes playing and the error is gone now. Thanks for the help @hirbod