就像标题说的,有人知道原因吗?
请注意,这不会发生在iOS 11上。
我的调试环境:
- Xcode 9.2
- iOS 9、10、11
我的代码:
在我的应用程序中,我正在尝试通过AVFoundation框架播放流媒体内容。显示视频的长度和当前播放时间也是一个功能。loadVideo
方法在 IBAction 中由于点击按钮而被调用。
此外,observeValueForKeyPath
方法中没有特殊的代码来接收 KVO 的事件。
详细信息如下:
- (void)loadVideo
{
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:VIDEO_URL]];
Float64 duration = CMTimeGetSeconds(item.asset.duration);
[self updateTimeLabel:0.0 duration:duration];
self.player = [[AVPlayer alloc] initWithPlayerItem:item];
AVPlayerLayer *layer = (AVPlayerLayer *)self.playerView.layer;
[layer setPlayer:self.player];
[self.player addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
__weak ViewController *weakSelf = self;
self.token = [self.player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(0.1, NSEC_PER_SEC)
queue:nil
usingBlock:^(CMTime time) {
Float64 currentTime = CMTimeGetSeconds(time);
[weakSelf updateTimeLabel:currentTime duration:duration];
}];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context
{
if ([keyPath isEqualToString:@"status"]) {
AVPlayer *player = (AVPlayer *)object;
switch (player.status) {
case AVPlayerStatusReadyToPlay:
{
NSLog(@"player's status changed to AVPlayerStatusReadyToPlay");
}
break;
default:
NSLog(@"player's status changed");
break;
}
[player removeObserver:self forKeyPath:@"status"];
}
}
远程内容不像本地内容那样工作。
在iOS 9中,您应该使用AVPlayerItem作为有关整个网络中AVAset到达和播放的信息位置,跟踪playbackLikelyToKeepUp
和accessLog
等属性,以及AVPlayerItemPlaybackStalled
等通知。
在iOS 10及更高版本中,您可以使用AVPlayer,但要注意的是它的timeControlStatus
。
你应该观察AVPlayerItem,而不是AVPlayer。
[self.item addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
苹果的文档