AVPlayer KVO crashes at replaceCurrentItem on iOS 16.1

Call function replaceCurrentPlayerItem of AVPlayer while running in the background and then throw an exception.

It seems to be not limited to currentItem.hasEnabledAudio. We saw similar exceptions for these members:

  1. currentItem.hasEnabledVideo
  2. currentItem.liveUpdateInterval
  3. currentItem.duration
  4. currentItem.status
  5. currentItem.seekableTimeRangesLastModifiedTime
Fatal Exception: NSInternalInconsistencyException Cannot remove an observer <NSKeyValueObservance 0x282f90900> for the key path "currentItem.liveUpdateInterval" from <AVPlayer 0x2823d2590>, most likely because the value for the key "currentItem" has changed without an appropriate KVO notification being sent. Check the KVO-compliance of the AVPlayer class. Expected behavior No crash

You can use AVQueuePlayer instead of AVPlayer.

Before

AVPlayerViewController *vc = ###whatever###;
AVPlayer *player = vc.player;
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]];
if (player) {
    [player replaceCurrentItemWithPlayerItem:playerItem];
} else {
    player = [AVPlayer playerWithPlayerItem:playerItem];
}

After

AVPlayerViewController *vc = ###whatever###;
AVPlayer *player = vc.player;
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL fileURLWithPath:path]];
if (player) {
    AVQueuePlayer *player = (AVQueuePlayer *)player;
    [player removeAllItems];
    [player insertItem:playerItem afterItem:nil];
} else {
    player = [AVQueuePlayer playerWithPlayerItem:playerItem];
}

see: https://github.com/SwiftOldDriver/iOS-Weekly/issues/3725

Experiencing the same crash on my side.

But why so many crashes on AVPlayer with iOS 16? Firstly, crash on [AVPlayer _addLayer] and now this crash?

Please file a report with a reproducible case attached.

Set allowsVideoFrameAnalysis = false on the AVPlayerViewController using this.

AVPlayer KVO crashes at replaceCurrentItem on iOS 16.1
 
 
Q