使用 Xcode 7.1.1 运行 El Capitan 开发 TVos 9.1 / IOS 9.2
我在 TVos 下运行此代码...
-(void)runVideo {
self.avPlayer = [AVPlayer playerWithURL:[NSURL URLWithString:videoHLS]];
avPlayerItem = self.avPlayer.currentItem;
[avPlayer.currentItem addObserver:self forKeyPath:@"status" opt ons:NSKeyValueObservingOptionNew context:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(itemDidFinishPlaying:) name:AVPlayerItemDidPlayToEndTimeNotification object:avPlayerItem];
playerViewController = [[AVPlayerViewController alloc] initWithNibName:nil bundle:nil];
playerViewController.player = avPlayer;
playerViewController.view.frame = self.view.frame;
[self addChildViewController:playerViewController];
[self.view addSubview:playerViewController.view];
[playerViewController didMoveToParentViewController:self];
}
它有效,当我打电话时......
dispatch_async(dispatch_get_main_queue(), ^(void) {
[playerViewController.player play];
});
它也有效,当我打电话时......
dispatch_async(dispatch_get_main_queue(), ^(void) {
[avPlayer play];
});
Bon,并且使用遥控器,它的行为也符合您的预期。但我想在代码中实现一个远程暂停按钮;我使用蓝牙做的,只有播放开始后我无法暂停播放。我尝试了所有这些,它们编译并且不会使播放器崩溃,它们似乎什么也没做。我设法做到的最好的就是完全杀死它,几乎没有停顿。
-(void)doPause {
//NSLog(@"self.playerViewController.player.rate %f",self.playerViewController.player.rate);
self.playerViewController.player.rate = 0.000000;
playerViewController.player.rate = 0.000000;
[self.playerViewController.player pause];
[self.avPlayer pause];
self.avPlayer.rate = 0.000000;
avPlayer.rate = 0.000000;
[self.avPlayer.currentItem cancelPendingSeeks];
[self.avPlayer.currentItem.asset cancelLoading];
self.playerViewController.player.rate = 0.000000;
[self.playerViewController player].rate = 0.000000;
// 这将杀死玩家并将我返回给父 VC
dispatch_async(dispatch_get_main_queue(), ^(void) {
[playerViewController willMoveToParentViewController:nil];
[playerViewController.view removeFromSuperview];
[playerViewController removeFromParentViewController];
});
}
这是正确的方法还是我应该做其他事情来达到这种效果?