- 给工程打开小窗模式
- 上代码。
#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
demo的录屏
RPReplay_Final1679722339