![](/img/trans.png)
[英]using KVO to observe WKWebView's URL property not work in iOS 10
[英]Why the KVO on 'status' property of AVPlayer not get called on iOS 9 and 10
如标题所述,有人知道原因吗?
请注意,它不会在iOS 11上发生。
我的调试环境:
我的代码:
在我的应用中,我试图通过AVFoundation框架播放流式内容。 显示视频的长度和当前播放时间也是一项功能。
由于点击了按钮,因此在IBAction中调用了loadVideo
方法。
另外, 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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.