#pragma mark - -- KVO监听
// 添加监听
- (void)addObserver
{
// 监听VLC对象属性(时间和播放)
[_player.player addObserver:self forKeyPath:@"remainingTime" options:0 context:nil];
[_player.player addObserver:self forKeyPath:@"isPlaying" options:0 context:nil];
}
// kvo监听回调
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
// 可以在这里设置显示的时间和进度
// position:VLCMediaPlayer对象中的进度比例,可用此值设置播放进度
// NSLog(@"[_player.player position]:%lf",[_player.player position]);
// remainingTime:VLCMediaPlayer对象中的剩余时间,stringValue可以转化为时间格式字符串
// NSLog(@"[[_player.player remainingTime] stringValue]:%@",[[_player.player remainingTime] stringValue]);
// time:VLCMediaPlayer对象中的当前时间
// NSLog(@"[[_player.player time] stringValue]:%@", [[_player.player time] stringValue]);
// NSLog(@"剩余的分钟:%@",[[_player.player remainingTime] minuteStringValue]);
// NSLog(@"播放的分钟:%@",[[_player.player time] minuteStringValue]);
// 记录当前进度
self.currentProgress = [_player.player position];
// 根据分钟计算播放的秒数(这里不够严格,还得加上秒数)
self.videoCurrentTime = [[[_player.player time] minuteStringValue] intValue] * 60;
// 根据剩余时间和已经播放的计算总秒数(这里不够严格,还得加上秒数)
self.videoAllTime = [[[_player.player remainingTime] minuteStringValue] intValue]*60 + self.videoCurrentTime;
// 有时候获取不到时间(个人想法是结合定时器和进度比例计算总时间等)
// ...
// 刷新最新时间和播放进度
[self updateTime];
// 停止菊花加载
if (self.activityView.isAnimating) {
[self.activityView stopAnimating];
}
}