Muj*_*aFR 7 iphone cocoa-touch ios avplayer ios9
我正在尝试使用最近在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;
Run Code Online (Sandbox Code Playgroud)
但是如何在屏幕中显示 _AVPictureInPictureController 呢??我试过
[self presentViewController:_AVPictureInPictureController animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
和
[self presentMoviePlayerViewControllerAnimated:_AVPictureInPictureController];
Run Code Online (Sandbox Code Playgroud)
但它没有用):
我也试过
[self.view.layer addSublayer: layer];
Run Code Online (Sandbox Code Playgroud)
但我只在屏幕上显示 AVPlayer 没有按钮或控件..
我曾经使用MPMoviePlayerViewController但由于它在 iOS 9 中正式弃用,我不能再使用它了..
那么你能帮我显示_AVPictureInPictureController 吗!!
提前致谢
当我检查有关AVPictureInPictureController的文档以及 Swift 中的 Apple 示例代码时AVPictureInPictureController。没有播放器。在文档中,您需要创建一个按钮。并且您需要检查当前视频是否已播放AVPictureInPictureController或不喜欢跟随。
拳头你需要设置AVAudioSessionCategoryPlayback。你需要为你的项目做 Xcode Capabilities 视图,在 Background Modes 部分选择 Audio and AirPlay。
在您的应用程序委托中,您需要设置以下代码:
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
[[AVAudioSession sharedInstance] setActive: YES error: nil];
Run Code Online (Sandbox Code Playgroud)
现在您需要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
}
}
Run Code Online (Sandbox Code Playgroud)
如果支持,这是 PIP 的按钮切换代码:
-(IBAction)actionPIPStart:(UIButton*)sender
{
if (_AVPictureInPictureController.pictureInPictureActive) {
[_AVPictureInPictureController stopPictureInPicture];
}
else {
[_AVPictureInPictureController startPictureInPicture];
}
}
Run Code Online (Sandbox Code Playgroud)
您现在可以检查其代表:
- (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;
Run Code Online (Sandbox Code Playgroud)
注意:以上代码仅用于示例,可能是您的视频屏幕未完全填充设备屏幕。
希望这些信息对您有所帮助,并且可能是您解决更深入阅读苹果文档的问题。AVPictureInPictureController