1. 工程打开小窗模式
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
  2. 代码
#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

原文地址:https://blog.csdn.net/u010919133/article/details/129765975

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_49100.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注