当我查看有关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