如何使用AVPlayer处理流错误

Dam*_*ito 4 error-handling streaming key-value-observing audio-streaming avplayer

我使用AVPlayer流跟踪.我正在尝试处理所有错误,如网络不可用或流不可用但我发现这种错误的任何处理程序.

我已经为avplayer的状态添加了一个KVO.

 [self.player addObserver:self forKeyPath:@"status" options:0 context:nil];
Run Code Online (Sandbox Code Playgroud)

但即使流不存在(例如:错误的url),状态也会切换到AVPlayerStatusReadyToPlay.

编辑

解决方案是使用AVPLayerItems并使用AVQueuPlayer.另一个问题是我在每个轨道上重新分配了这个播放器.

Lea*_*Bun 7

使用NSKeyValueObservingOptionNew将KVO添加到AVPlayerItem而不是AVPlayer.

[playerItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:NULL];
[self.player replaceCurrentItemWithPlayerItem:playerItem];
Run Code Online (Sandbox Code Playgroud)