如标题所示,有谁知道原因吗?
请注意,它不会发生在 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 作为有关通过网络到达和播放 AVAsset 的信息的位置,跟踪诸如 playbackLikelyToKeepUp
和 accessLog
,以及 AVPlayerItemPlaybackStalled
等通知。
在 iOS 10 及更高版本中,您可以使用 AVPlayer,但需要注意的是它的 timeControlStatus
。
关于ios - 为什么在 iOS 9 和 10 上无法调用 AVPlayer 的 'status' 属性上的 KVO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48367599/