Picture in picture(pip AVPictureInPictureController)

实现pip过程:
https://developer.apple.com/documentation/avkit/adopting_picture_in_picture_in_a_custom_player?language=objc

https://stackoverflow.com/questions/32667090/how-to-display-avpictureinpicturecontroller

Fist you need to set up AVAudioSessionCategoryPlayback. you need to do the Xcode Capabilities view for your project, select Audio and AirPlay in the Background Modes section.
1、在AppDelegate.m下

#import "AppDelegate.h"
#import "AVFoundation/AVFoundation.h"
@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];
    
    return YES;
}

2、生成完player之后将playerLayer给pipcontroller,

@property (nonatomic, retain) AVPictureInPictureController *pipController;
-(void)setupSuport
{
    if([AVPictureInPictureController isPictureInPictureSupported])
    {
        printf("lee setupSuport\n");
        self.pipController =  [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerLayer];
        self.pipController.delegate = self;
    }
    else
    {
        // not supported PIP start button desable here
    }
    
}
//监听pip
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController restoreUserInterfaceForPictureInPictureStopWithCompletionHandler:(void (^)(BOOL restored))completionHandler{
    printf("lee restoreUser\n");
}
- (void)pictureInPictureControllerDidStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController{
    printf("lee DidStop\n");
}
- (void)pictureInPictureControllerDidStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController{
    printf("lee DidStart\n");
}
- (void)pictureInPictureControllerWillStopPictureInPicture:(AVPictureInPictureController *)pictureInPictureController{
    printf("lee WillStop\n");
}
- (void)pictureInPictureController:(AVPictureInPictureController *)pictureInPictureController failedToStartPictureInPictureWithError:(NSError *)error{
    printf("lee failedToStart\n");
}
- (void)pictureInPictureControllerWillStartPictureInPicture:(AVPictureInPictureController *)pictureInPictureController{
    printf("lee WillStart\n");
}

3、建立点击事件 pip响应

- (IBAction)pipBtnClick:(id)sender {
    NSLog(@"lee pipBtnClick\n");
    
    if (self.pipController.pictureInPictureActive) {
        [self.pipController stopPictureInPicture];
    }
    else {
        [self.pipController startPictureInPicture];
         NSLog(@"lee pipBtnClick--start\n");
    }
}

ps.按home键没有成功pip


image.png

ps:已解决 2019/10/18
https://www.jianshu.com/writer#/notebooks/36127066/notes/52619102

你可能感兴趣的:(Picture in picture(pip AVPictureInPictureController))