10

I am using AVPLayerViewController to stream content. Playback works fine but I don't see "Done" button to dismiss the view.

Here is I how I setup and show the view controller

NSURL* url = [content localSessionUrl];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:url];
[self.navigationController pushViewController:playerViewController animated:YES];
[self.navigationController setNavigationBarHidden:YES];

Is it because I am pushing it on a navigationcontroller? enter image description here

dev
  • 2,180
  • 2
  • 30
  • 46

5 Answers5

24

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];
}
Elijah
  • 8,381
  • 2
  • 55
  • 49
pkc456
  • 8,350
  • 38
  • 53
  • 109
7

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.

Tim
  • 8,932
  • 4
  • 43
  • 64
3

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...

bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
0

For iPad I use UIModalPresentationFormSheet for presenting and done button wwas missing... so I used my own button:

        AVPlayerViewController *playerViewController =
 [[AVPlayerViewController alloc] init]; 

//...

        UIViewController *controllerToPresent = playerViewController;

        if (IS_IPAD){
            SBNavigationController *nc = [[SBNavigationController alloc] initWithRootViewController:playerViewController];
            [playerViewController.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"") style:UIBarButtonItemStylePlain target:self action:@selector(cancelPlayingPressed)]];
            controllerToPresent = nc;
        } 
Marek Manduch
  • 2,303
  • 1
  • 19
  • 22
0

I ran into this issue as well. I switched my segue from "Push" to "Modal" and the close button appeared.

enter image description here

Casper
  • 3,765
  • 2
  • 13
  • 12