如何显示AVPictureInPictureController?

How to display AVPictureInPictureController?

我正在尝试使用最近在 IOS9 中引入的 AVPictureInPictureController 播放视频:

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;

但是如何在屏幕上显示_AVPictureInPictureController?? 我试过了

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

[self presentMoviePlayerViewControllerAnimated:_AVPictureInPictureController];

但是没用 ):

我也试过了

[self.view.layer addSublayer: layer];

但我在屏幕上只显示 AVPlayer,没有按钮或控件..

我曾经使用 MPMoviePlayerViewController 但由于它在 iOS 9 中被正式弃用,我不能再使用它了..

你能帮我显示 _AVPictureInPictureController !!

提前致谢

当我查看有关 AVPictureInPictureController 的文档以及 Swift 中有关 AVPictureInPictureController 的 Apple 示例代码时。没有播放器。在文档中,您需要创建一个按钮。并且您需要检查当前视频是否播放 AVPictureInPictureController 或不喜欢以下内容。

拳头你需要设置AVAudioSessionCategoryPlayback。您需要为您的项目执行 Xcode 功能视图,select 背景模式部分中的音频和 AirPlay

在您的应用委托中,您需要设置以下代码:

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

现在您需要使用以下代码编写使用 AVPlayer 播放视频的代码:

- (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
    }

}

如果支持,这里是 PIP 的按钮切换代码:

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

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

您现在可以与它的代表核实:

- (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;

注意:以上代码仅供示例,可能是您的视频屏幕未完全填满设备屏幕。

希望这些信息对您有所帮助,并且可能是您需要解决的问题,以便更深入地阅读 apple doc。 AVPictureInPictureController

直接使用AVPlayerViewController,详情请查看https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/AdoptingMultitaskingOniPad/QuickStartForPictureInPicture.html