Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display AVPictureInPictureController?

I'm trying to play video using AVPictureInPictureController which was introduced recently in IOS9, using this code :

AVPlayer *_AVPlayer = [AVPlayer playerWithURL:url];
AVPlayerLayer *_layer = [AVPlayerLayer playerLayerWithPlayer:_AVPlayer];
_layer.frame = [[UIApplication sharedApplication] delegate].window.bounds;
AVPictureInPictureController *_AVPictureInPictureController = [[AVPictureInPictureController alloc] initWithPlayerLayer:_layer];
_AVPictureInPictureController.delegate = self;

But how can I display the _AVPictureInPictureController in the screen?? I tried

[self presentViewController:_AVPictureInPictureController animated:YES completion:nil];

and

[self presentMoviePlayerViewControllerAnimated:_AVPictureInPictureController];

But it didn't work ):

Also I tried

[self.view.layer addSublayer: layer];

But i shows only AVPlayer on the screen without buttons or controls ..

I used to work with MPMoviePlayerViewController but as it is formally deprecated in iOS 9, I can't use it any more ..

So could You help me to display the _AVPictureInPictureController !!

Thanks in advance

like image 772
MujtabaFR Avatar asked Sep 19 '15 10:09

MujtabaFR


People also ask

Is it possible to subclass avpictureinpicturecontroller?

The framework doesn’t support subclassing AVPictureInPictureController. Creates a Picture in Picture controller with a content source. Creates a Picture in Picture controller with a player layer. var contentSource: AVPictureInPictureController.ContentSource? The source of the controller’s content.

How do I call avpictureinpicturecontroller from a view model?

The Coordinator subscribes to the view model's publisher $isInPipMode so that it can call the AVPictureInPictureController methods startPictureInPicture (), if the published value is true, and stopPictureInPicture (), if it is false.

What is a picture in picture controller?

A controller that responds to user-initiated Picture in Picture playback of video in a floating, resizable window. To use Picture in Picture, you need to configure your app to support background audio playback.

How to start picture in picture using the user interface?

Before presenting a user interface to start Picture in Picture, call the isPictureInPictureSupported () method to determine if the current device supports the feature, and check the isPictureInPicturePossible property value to determine whether PiP is possible in the current context.


1 Answers

As i checking the documentation about AVPictureInPictureController and also Apple sample code in Swift about AVPictureInPictureController. there is not player. in doc you need to create a Button. and that you need to check for the current video is sported AVPictureInPictureController or not like following.

Fist you need to set up AVAudioSessionCategoryPlayback. you need to do the Xcode Capabilities view for your project, select Audio and AirPlay in the Background Modes section.

In to you app delegate you need to set following code:

  [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
  [[AVAudioSession sharedInstance] setActive: YES error: nil];

Now You need to do code for playing video using AVPlayer using following code:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *path = [[NSBundle mainBundle] pathForResource:@"samplemovie" ofType:@"mov"];
    NSURL *url = [NSURL fileURLWithPath:path];
    self.AVPlayer = [AVPlayer playerWithURL:url];
    self.layer = [AVPlayerLayer playerLayerWithPlayer:_AVPlayer];
    //_layer.frame = [[UIApplication sharedApplication] delegate].window.bounds;

    [self.layer setFrame:CGRectMake(0, 0, self.view.frame.size.width-100, self.view.frame.size.height-72)];
    [self.layer setVideoGravity:AVLayerVideoGravityResize];
    [self.view.layer addSublayer:_layer];

    [_AVPlayer play];
    [self setupSuport];   //Here you need to check that `AVPictureInPictureController` is supported or not with another method


}


-(void)setupSuport
{
    if([AVPictureInPictureController isPictureInPictureSupported])
    {

      _AVPictureInPictureController =  [[AVPictureInPictureController alloc] initWithPlayerLayer:_layer];
        _AVPictureInPictureController.delegate = self;

    }
    else
    {        
     // not supported PIP start button desable here
    }

}

Here is The button toggle code for PIP if supported:

-(IBAction)actionPIPStart:(UIButton*)sender
{

    if (_AVPictureInPictureController.pictureInPictureActive) {
        [_AVPictureInPictureController stopPictureInPicture];
    }
    else {
        [_AVPictureInPictureController startPictureInPicture];
    }
}

You can get now check with its delegate:

- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler;
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error;
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController;

NOTE: Above code is just for sample and might be your video screen not be full fill in screen of device.

Hope that information helps to you and might be your issue goign to be solve for more deep read apple doc. AVPictureInPictureController

like image 160
Nitin Gohel Avatar answered Nov 15 '22 00:11

Nitin Gohel