No "Done" button on AVPlayerViewController

Instead of pushing or add child view, try to present the view. The Done button will automatically shown. Below is the code snippet.

-(void)playVideoWithUrl:(NSURL *)url{
    AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc]init];
    playerViewController.player = [[AVPlayer alloc]initWithURL:url];
    [self presentViewController:playerViewController animated:YES completion:nil];
    playerViewController.view.frame = self.view.frame;
    [playerViewController.player play];
}

I think the issue is when you add the AVPlayerViewController view to the a view on the screen that takes up the whole screen, it treats it as already in fullscreen mode. The trick is to add it to a view that takes up a smaller portion of the screen.

Initially I did this:

// Setup an AVPlayerViewController
self.avPlayerViewController = [[AVPlayerViewController alloc] init];
if ([self.avPlayerViewController respondsToSelector:@selector(allowsPictureInPicturePlayback)]) {
    self.avPlayerViewController.allowsPictureInPicturePlayback = NO;
}

// Attach the Active Cloack AVPlayer to the AVPlayerViewController
AVPlayer *player = self.avPlayer;
self.avPlayerViewController.player = player;
self.avPlayerViewController.view.frame = self.view.bounds;
[self.view addSubview:self.avPlayerViewController.view];

// Setup the Parent Child relationshipo
[self addChildViewController:self.avPlayerViewController];
[self.avPlayerViewController willMoveToParentViewController:self];
[self.avPlayerViewController didMoveToParentViewController:self];

However, I then had the same problem that my player didn't have a Done button, so I couldn't exit the video.

I then modified my xib/storyboard and added a UINavigationBar to the top with a done button, and a UIView as a container underneath this. I then added the avPlayerViewController to this container view and the controls magically changed into a none full screen mode. I got a full screen button to take the player into full screen mode.

However I then had to create code to auto hide my nav bar and add a done button to this and a title etc...


Yes, the 'Done' button acts as a dismissal for a modal presentation style. If you are pushing the AVPlayerViewController onto the navigation stack you will need to handle the popping yourself.

Tags:

Ios