我正在尝试使用原生播放器(AVPlayer)在iOS上重现直播。但是,我无法恢复播放。当我停止播放并在几秒钟后恢复播放时,播放从我暂停的那一刻开始,而不是再现当前流的当前(最后)样本。
有没有办法获取最后一个样本,o配置AVPlayer在点击播放按钮时从上一个样本重现?
答案 0 :(得分:0)
我的解决方案是基于拒绝用户让玩家暂停。这是,每次重放播放时都会摧毁播放器。每次创建一个新实例时,都要恢复播放。
根据Apple的建议,了解AVPlayer是否已停止的唯一解决方案是添加KVO。这是:
- (void)setupPlayer {
AVPlayer *player = [AVPlayer playerWithURL:streamURL];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = player;
self.playerViewController = playerViewController;
[self configureConstraintsForView:self.playerViewController.view]; //Add Player to View
[self setupObservers];
[player play];
}
- (void)setupObservers {
[self.playerViewController.player addObserver:self
forKeyPath:@"rate"
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
context:NULL];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey, id> *)change context:(void *)context
if ([keyPath isEqualToString:@"rate"] && self.playerViewController.player.rate == CGPointZero.x) {
[self.playerViewController.view removeFromSuperview];
[self.playerViewController.player removeObserver:self forKeyPath:@"rate"];
[self.playerViewController.player pause];
self.playerViewController = nil;
}
}
然后,当用户想要重新吸引播放器时,只需拨打-(void)setupPlayer
即可从上一个实时样本开始播放。