如何显示AVPictureInPictureController?

9
我正在尝试使用最近在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吗!!
提前致谢
2个回答

8

我正在查看有关 AVPictureInPictureController 的文档,以及有关 AVPictureInPictureController 的Swift的Apple示例代码。文档中没有播放器。您需要创建一个按钮,并检查当前视频是否支持AVPictureInPictureController,如下所示。

首先,您需要设置AVAudioSessionCategoryPlayback。您需要在项目的Xcode功能视图中选择“背景模式”部分中的“音频和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;

注意:以上代码仅为示例,您的视频屏幕可能不会完全填充设备屏幕。

希望这些信息对您有所帮助,并且可能解决您的问题。要深入阅读苹果文档,请访问AVPictureInPictureController


我无法使其工作...它总是显示不可能...你能提供一个可用的示例代码吗? - Fabiosoft
非常奇怪...我将当前代码复制/粘贴到一个简单的空项目中...没有任何效果。按 PiP 按钮,但只有一个效果...我做错了什么? - Genevios

-4

1
有时候你不能依赖默认的用户界面。 - Fabiosoft

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接