#import <AVKit/AVKit.h>
@interface ViewController () <AVPictureInPictureControllerDelegate>
@property (nonatomic, strong) AVPictureInPictureController *pipVC;
@property (nonatomic, strong) AVPlayerLayer *playerLayer;
@property (nonatomic, strong) AVPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = UIColor.whiteColor;
//让视频在静音模式下也能支持播放声音,以及打开小窗
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
//你本地工程里面的一个视频,本例视频名称为hubblecast.m4v
NSURL *localUrl = [[NSBundle mainBundle] URLForResource:@"hubblecast" withExtension:@"m4v"];
self.player = [AVPlayer playerWithURL:localUrl];
self.playerLayer = [AVPlayerLayer playerLayerWithPlayer:self.player];
self.playerLayer.frame = CGRectMake(10, 100, 400, 200);
[self.player play];
[self.view.layer addSublayer:self.playerLayer];
//判断是否支持画中画功能
if ([AVPictureInPictureController isPictureInPictureSupported]) {
self.pipVC = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerLayer];
self.pipVC.delegate = self;
}
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 360, 400, 80)];
[button setTitle:@"开启/关闭小窗" forState:UIControlStateNormal];
[button addTarget:self action:@selector(openOrClosePictureInPicture:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = UIColor.redColor;
[self.view addSubview:button];
}
- (void)openOrClosePictureInPicture:(UIButton *)button {
if (self.pipVC.isPictureInPictureActive) {
[self.pipVC stopPictureInPicture];
} else {
[self.pipVC startPictureInPicture];
}
}
#pragma mark - AVPictureInPictureControllerDelegate
/*!
@method pictureInPictureControllerWillStartPictureInPicture:
@param pictureInPictureController
The Picture in Picture controller.
@abstract Delegate can implement this method to be notified when Picture in Picture will start.
*/
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
NSLog(@"%s", __func__);
}
/*!
@method pictureInPictureControllerDidStartPictureInPicture:
@param pictureInPictureController
The Picture in Picture controller.
@abstract Delegate can implement this method to be notified when Picture in Picture did start.
*/
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
NSLog(@"%s", __func__);
}
/*!
@method pictureInPictureController:failedToStartPictureInPictureWithError:
@param pictureInPictureController
The Picture in Picture controller.
@param error
An error describing why it failed.
@abstract Delegate can implement this method to be notified when Picture in Picture failed to start.
*/
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error {
NSLog(@"%s", __func__);
}
/*!
@method pictureInPictureControllerWillStopPictureInPicture:
@param pictureInPictureController
The Picture in Picture controller.
@abstract Delegate can implement this method to be notified when Picture in Picture will stop.
*/
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
NSLog(@"%s", __func__);
}
/*!
@method pictureInPictureControllerDidStopPictureInPicture:
@param pictureInPictureController
The Picture in Picture controller.
@abstract Delegate can implement this method to be notified when Picture in Picture did stop.
*/
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController {
NSLog(@"%s", __func__);
}
/*!
@method pictureInPictureController:restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:
@param pictureInPictureController
The Picture in Picture controller.
@param completionHandler
The completion handler the delegate needs to call after restore.
@abstract Delegate can implement this method to restore the user interface before Picture in Picture stops.
*/
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler {
NSLog(@"%s", __func__);
}
@end
RPReplay_Final1679722339
原文地址:https://blog.csdn.net/u010919133/article/details/129765975
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_49100.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。