ios 如何显示AVPictureInPictureController?

vawmfj5a  于 2023-06-25  发布在  iOS
关注(0)|答案(2)|浏览(169)

我尝试使用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**!!
先谢谢你了

j13ufse2

j13ufse21#

当我检查有关AVPictureInPictureController的文档以及Apple在Swift中关于AVPictureInPictureController的示例代码时。没有玩家。在Docs中,您需要创建一个Button。而你需要检查当前的视频是sported 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];
    }
}

您现在可以使用它的委托获得check:

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

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

希望这些信息对你有帮助,可能是你的问题goign得到解决,以便更深入地阅读苹果文档。AVPictureInPictureController

相关问题