![](/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.