Search code examples
iosavplayer

Can I add custom view on AVPictureInPictureController?


I found an interesting app which pip is not a video but a label:

enter image description here

I had try add custom layer on AVPlayerLayer, but it not work.

Any idea to achieve it?


Solution

  • You can get the picture-in-picture window in AVPictureInPictureControllerDelegate and then add your views:

    Swift:

    func pictureInPictureControllerWillStartPictureInPicture(_ pictureInPictureController: AVPictureInPictureController) {
        // note this is first window
        if let window = UIApplication.shared.windows.first {
            window.addSubview(customView)
            // use autoLayout
            customView.snp.makeConstraints { (make) -> Void in
                make.edges.equalToSuperview()
            }
        }
    }
    

    Objective-C:

    - (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
        UIWindow *firstWindow = [UIApplication sharedApplication].windows.firstObject;
        [firstWindow addSubview:self.customView];
        [self.customView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(firstWindow);
        }];
    }