0

Running El Capitan with Xcode 7.1.1 developing TVos 9.1 / IOS 9.2

I running this code under 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];
}  

It works, when I call ...

dispatch_async(dispatch_get_main_queue(), ^(void) {
            [playerViewController.player play];
        });

It also works, when I call ...

dispatch_async(dispatch_get_main_queue(), ^(void) {
            [avPlayer play];
        });

Bon, and using the remote it behaves as you would expect it too. But I want to implement a remote pause button in code; which I do using bluetooth, only I am unable to pause playback once it has began. I tried all of these, which compile and don't crash the player, they do nothing it seems. The best I managed is to completely kill it, hardly a pause.

-(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;

// This will kill the player and return me to the parent VC

dispatch_async(dispatch_get_main_queue(), ^(void) {
    [playerViewController willMoveToParentViewController:nil];
    [playerViewController.view removeFromSuperview];
    [playerViewController removeFromParentViewController];
});
}

Is this the right approach or should I be doing something else to achieve this effect?

user3069232
  • 8,587
  • 7
  • 46
  • 87

1 Answers1

0

I think I found the answer to this question here.

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

It seems playerViewController as an in-built auto-restart so that even if I do pause it, it immediately restarts. Have noticed the menu pop'ing up and then disappearing when I'm trying to pause.

Which leaves me with one option. Kill the playerViewController.

user3069232
  • 8,587
  • 7
  • 46
  • 87
  • Yes, I confirmed it. I simply used the AVPlayer + AVPlayerLayer and I had far more control, of course now I need to implement all the controls, which is evidently more difficult than you might think. – user3069232 Jan 10 '16 at 20:38