我正在尝试使用最近在 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 !!
提前致谢
答案 0 :(得分:4)
当我查看关于AVPictureInPictureController的文档以及关于AVPictureInPictureController
的Swift中的Apple示例代码时。没有球员。在doc中,您需要创建一个Button。并且您需要检查当前视频是否为AVPictureInPictureController
或不符合以下内容。
拳头你需要设置AVAudioSessionCategoryPlayback
。您需要为项目执行Xcode功能视图,在后台模式部分选择音频和AirPlay 。
在您的app appate中,您需要设置以下代码:
[[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
答案 1 :(得分:-1)
只需使用AVPlayerViewController instread,详情请查看https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/AdoptingMultitaskingOniPad/QuickStartForPictureInPicture.html