iPad 画中画,AVPictureInPictureController使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

[self avkitPlayer];
}


- (void)avkitPlayer{

NSString *path = [[NSBundle mainBundle] pathForResource:@"movie" ofType:@"mp4"];
self.avPlayer = [AVPlayer playerWithURL:[NSURL fileURLWithPath:path]];
self.playerlayer = [AVPlayerLayer playerLayerWithPlayer:self.avPlayer];
self.playerlayer.frame = self.view.bounds;
if([AVPictureInPictureController isPictureInPictureSupported]){
_avPictureInPictureController = [[AVPictureInPictureController alloc] initWithPlayerLayer:self.playerlayer];
_avPictureInPictureController.delegate = self;
}
[self.avPlayer play];
[self.view.layer addSublayer:self.playerlayer];

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


- (IBAction)actionPiPStart:(id)sender {
if (_avPictureInPictureController.pictureInPictureActive) {
[_avPictureInPictureController stopPictureInPicture];
}
else {
[_avPictureInPictureController startPictureInPicture];
}
}