gpt4 book ai didi

ios - 如何显示AVPictureInPictureController?

转载 作者:可可西里 更新时间:2023-11-01 05:43:33 34 4
gpt4 key购买 nike

我正在尝试使用最近在 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 的文档时以及关于 AVPictureInPictureController 的 Apple Swift 示例代码。没有播放器。在文档中,您需要创建一个按钮。并且您需要检查当前视频是否播放 AVPictureInPictureController 或不喜欢以下内容。

首先你需要设置AVAudioSessionCategoryPlayback。您需要为您的项目执行 Xcode 功能 View ,在后台模式部分选择音频和 AirPlay

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

  [[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

关于ios - 如何显示AVPictureInPictureController?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32667090/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com