iosiphonecocoa-touchavplayerios9

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


Solution

  • 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