无法观察AVPlayerItem的@“状态”键

我试图从URL播放stream,但这是一个问题。 ObserveValueForKeyPath:ofObject:change:context只是不执行。 据我所知,这不取决于streamURL,不pipe它是否正确。 它必须将状态从AVPlayerItemStatusUnknown更改为AVPlayerItemStatusReadyToPlay或AVPlayerItemStatusFailed。 但是,尽pipe如此,我在浏览器中检查了该url,并且工作正常。 所以有什么问题? 我观看了WWDC的video,它说,如果你有audiostream,在AVAsset 之前使用AVPLayerItem。

这是我的代码:

- (void) prepare { NSURL * trackStreamURL = [NSURL URLWithString:streamURL]; NSLog(@"STREAM URL: %@",trackStreamURL); _playerItem = [AVPlayerItem playerItemWithURL:trackStreamURL]; [_playerItem addObserver:self forKeyPath:@"status" options:0 context:nil]; } - (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { NSLog(@"KEY PATH : %@", keyPath); NSLog(@"CHANGE : %@", change); } 

提前致谢!

我总是在AVPlayer上观察status属性,而不是AVPlayerItem因为你需要一个玩家来玩玩家项目。 事实上,我从来没有看过玩家物品的状态属性。

玩家项目的状态可能永远不会改变,因为没有玩家准备好玩它。 所以,用玩家项目创build一个玩家并观察玩家的状态(或者可能仍然是玩家项目的状态)。

 AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];