Skip to content

Commit

Permalink
Merge pull request renzifeng#1092 from xxxIxxxx/dev_pip
Browse files Browse the repository at this point in the history
暴漏AVPlayerLayer,为提供给画中画使用,增加简单的画中画开启样例
  • Loading branch information
renzifeng committed Nov 4, 2020
2 parents abf9ccd + b7f8b92 commit 7bea5cf
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Example/ZFPlayer/Main/ZFViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ - (void)viewDidLoad {
}

- (NSArray <ZFTableItem *>*)createItemsByPlayerType {
return @[[ZFTableItem itemWithTitle:@"普通样式" subTitle:@"Normal style" viewControllerName:@"ZFNormalViewController"],
return @[[ZFTableItem itemWithTitle:@"普通样式,画中画" subTitle:@"Normal style" viewControllerName:@"ZFNormalViewController"],
[ZFTableItem itemWithTitle:@"UITableView样式" subTitle:@"UITableView style" viewControllerName:@"ZFAutoPlayerViewController"],
[ZFTableItem itemWithTitle:@"UICollectionView样式" subTitle:@"UICollectionView style" viewControllerName:@"ZFCollectionViewController"],
[ZFTableItem itemWithTitle:@"UIScrollView样式" subTitle:@"UIScrollView style" viewControllerName:@"ZFScrollViewViewController"]];
Expand Down
37 changes: 36 additions & 1 deletion Example/ZFPlayer/Normal/ZFNormalViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import <ZFPlayer/UIImageView+ZFCache.h>
#import <ZFPlayer/ZFPlayerConst.h>
#import "ZFUtilities.h"
@import AVKit;

static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";

Expand All @@ -25,8 +26,9 @@ @interface ZFNormalViewController ()
@property (nonatomic, strong) UIButton *playBtn;
@property (nonatomic, strong) UIButton *changeBtn;
@property (nonatomic, strong) UIButton *nextBtn;
@property (nonatomic, strong) UIButton *pipBtn;
@property (nonatomic, strong) NSArray <NSURL *>*assetURLs;

@property (nonatomic, strong) AVPictureInPictureController *vc;
@end

@implementation ZFNormalViewController
Expand All @@ -41,6 +43,7 @@ - (void)viewDidLoad {
[self.containerView addSubview:self.playBtn];
[self.view addSubview:self.changeBtn];
[self.view addSubview:self.nextBtn];
[self.view addSubview:self.pipBtn];
[self setupPlayer];
}

Expand Down Expand Up @@ -79,6 +82,13 @@ - (void)viewWillLayoutSubviews {
x = (CGRectGetWidth(self.view.frame)-w)/2;
y = CGRectGetMaxY(self.changeBtn.frame)+50;
self.nextBtn.frame = CGRectMake(x, y, w, h);


w = 100;
h = 30;
x = (CGRectGetWidth(self.view.frame)-w)/2;
y = CGRectGetMaxY(self.nextBtn.frame)+50;
self.pipBtn.frame = CGRectMake(x, y, w, h);
}

- (void)setupPlayer {
Expand Down Expand Up @@ -114,6 +124,22 @@ - (void)setupPlayer {
self.player.assetURLs = self.assetURLs;
[self.player playTheIndex:0];
[self.controlView showTitle:@"iPhone X" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeAutomatic];





}

- (void)picBtnClick {
/// 配置画中画
ZFAVPlayerManager *manager = (ZFAVPlayerManager *)self.player.currentPlayerManager;
AVPictureInPictureController *vc = [[AVPictureInPictureController alloc] initWithPlayerLayer:manager.avPlayerLayer];
self.vc = vc;
///要有延迟 否则可能开启不成功
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2.0*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
[self.vc startPictureInPicture];
});
}

- (void)changeVideo:(UIButton *)sender {
Expand Down Expand Up @@ -209,6 +235,15 @@ - (UIButton *)nextBtn {
return _nextBtn;
}

- (UIButton *)pipBtn {
if (!_pipBtn) {
_pipBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[_pipBtn setTitle:@"开启画中画" forState:UIControlStateNormal];
[_pipBtn addTarget:self action:@selector(picBtnClick) forControlEvents:UIControlEventTouchUpInside];
}
return _pipBtn;
}

- (NSArray<NSURL *> *)assetURLs {
if (!_assetURLs) {
_assetURLs = @[[NSURL URLWithString:@"https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4"],
Expand Down
2 changes: 2 additions & 0 deletions ZFPlayer/Classes/AVPlayer/ZFAVPlayerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,6 @@
/// 视频请求头
@property (nonatomic, strong) NSDictionary *requestHeader;

@property (nonatomic, strong, readonly) AVPlayerLayer *avPlayerLayer;

@end
5 changes: 5 additions & 0 deletions ZFPlayer/Classes/AVPlayer/ZFAVPlayerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,11 @@ - (ZFPlayerView *)view {
return _view;
}

- (AVPlayerLayer *)avPlayerLayer {
ZFPlayerPresentView *view = (ZFPlayerPresentView *)self.view.playerView;
return [view avLayer];
}

- (float)rate {
return _rate == 0 ?1:_rate;
}
Expand Down

0 comments on commit 7bea5cf

Please sign in to comment.