【问题标题】:How to display AVPictureInPictureController?如何显示 AVPictureInPictureController?
【发布时间】:2015-12-16 11:49:27
【问题描述】:

我正在尝试使用最近在 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吗!!

提前致谢

【问题讨论】:

    标签: ios iphone cocoa-touch avplayer ios9


    【解决方案1】:

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

    您需要先设置AVAudioSessionCategoryPlayback。您需要为您的项目执行 Xcode Capabilities 视图,在 Background Modes 部分中选择 Audio and 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

    【讨论】:

    • 我无法让它工作......它总是显示不可能......你能链接一个示例工作代码吗?
    • 很奇怪...我将当前代码复制/粘贴到简单的空项目...没有任何效果。画中画的按钮,但只有一个效果......我做错了什么?
    【解决方案2】:

    【讨论】:

    • 有时你不能依赖默认用户界面
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-20
    • 2018-06-27
    • 2021-05-10
    • 2012-04-29
    • 2012-11-03
    相关资源
    最近更新 更多