0

使用 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];
});
}

这是正确的方法还是我应该做其他事情来达到这种效果?

4

1 回答 1

0

我想我在这里找到了这个问题的答案。

https://forums.developer.apple.com/message/81573#81573

似乎 playerViewController 是一个内置的自动重启功能,因此即使我暂停它,它也会立即重启。当我试图暂停时,注意到菜单弹出然后消失。

这让我只有一个选择。杀死 playerViewController。

于 2016-01-10T14:28:46.713 回答