ZFADViewController.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //
  2. // ZFADViewController.m
  3. // ZFPlayer_Example
  4. //
  5. // Created by 紫枫 on 2019/6/5.
  6. // Copyright © 2019 紫枫. All rights reserved.
  7. //
  8. // Temporarily disable third-party advertising module
  9. #if 0
  10. #import "ZFADViewController.h"
  11. #import <ZFPlayer/ZFAVPlayerManager.h>
  12. #import <ZFPlayer/ZFIJKPlayerManager.h>
  13. #import <ZFPlayer/ZFPlayerControlView.h>
  14. #import <ZFPlayer/UIView+ZFFrame.h>
  15. #import <ZFPlayer/ZFPlayerConst.h>
  16. #import "UIImageView+ZFCache.h"
  17. #import "ZFUtilities.h"
  18. #import "ZFADControlView.h"
  19. static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
  20. @interface ZFADViewController ()
  21. @property (nonatomic, strong) ZFPlayerController *player;
  22. @property (nonatomic, strong) UIImageView *containerView;
  23. @property (nonatomic, strong) ZFPlayerControlView *controlView;
  24. @property (nonatomic, strong) ZFADControlView *adControlView;
  25. @property (nonatomic, strong) ZFAVPlayerManager *playerManager;
  26. @property (nonatomic, strong) ZFAVPlayerManager *adPlayerManager;
  27. @end
  28. @implementation ZFADViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. self.view.backgroundColor = [UIColor whiteColor];
  32. [self.view addSubview:self.containerView];
  33. CGFloat x = 0;
  34. CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  35. CGFloat w = CGRectGetWidth(self.view.frame);
  36. CGFloat h = w*9/16;
  37. self.containerView.frame = CGRectMake(x, y, w, h);
  38. self.playerManager = [[ZFAVPlayerManager alloc] init];
  39. /// 广告
  40. self.adPlayerManager = [[ZFAVPlayerManager alloc] init];
  41. /// 播放器相关
  42. self.player = [ZFPlayerController playerWithPlayerManager:self.adPlayerManager containerView:self.containerView];
  43. self.player.controlView = self.adControlView;
  44. /// 设置退到后台继续播放
  45. self.player.pauseWhenAppResignActive = NO;
  46. @zf_weakify(self)
  47. /// 播放完成
  48. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  49. @zf_strongify(self)
  50. if (self.player.currentPlayerManager == self.adPlayerManager) {
  51. self.player.controlView = self.controlView;
  52. self.player.currentPlayerManager = self.playerManager;
  53. self.player.currentPlayerManager.shouldAutoPlay = YES;
  54. [self.player.currentPlayerManager play];
  55. [self.controlView showTitle:@"iPhone X" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
  56. } else {
  57. [self.player stop];
  58. }
  59. };
  60. /// 一定要在player初始化之后设置assetURL
  61. self.playerManager.shouldAutoPlay = NO;
  62. self.playerManager.assetURL = [NSURL URLWithString:@"https://www.apple.com/105/media/cn/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/bruce/mac-bruce-tpl-cn-2018_1280x720h.mp4"];
  63. self.adPlayerManager.assetURL = [NSURL URLWithString:@"https://fcvideo.cdn.bcebos.com/smart/f103c4fc97d2b2e63b15d2d5999d6477.mp4"];
  64. self.adPlayerManager.shouldAutoPlay = YES;
  65. }
  66. - (void)viewWillLayoutSubviews {
  67. [super viewWillLayoutSubviews];
  68. CGFloat x = 0;
  69. CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  70. CGFloat w = CGRectGetWidth(self.view.frame);
  71. CGFloat h = w*9/16;
  72. self.containerView.frame = CGRectMake(x, y, w, h);
  73. }
  74. - (UIStatusBarStyle)preferredStatusBarStyle {
  75. return UIStatusBarStyleDefault;
  76. }
  77. - (BOOL)prefersStatusBarHidden {
  78. return NO;
  79. }
  80. - (BOOL)shouldAutorotate {
  81. return NO;
  82. }
  83. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  84. return UIInterfaceOrientationMaskPortrait;
  85. }
  86. - (ZFPlayerControlView *)controlView {
  87. if (!_controlView) {
  88. _controlView = [ZFPlayerControlView new];
  89. _controlView.fastViewAnimated = YES;
  90. _controlView.autoHiddenTimeInterval = 5;
  91. _controlView.autoFadeTimeInterval = 0.5;
  92. _controlView.prepareShowLoading = YES;
  93. }
  94. return _controlView;
  95. }
  96. - (ZFADControlView *)adControlView {
  97. if (!_adControlView) {
  98. _adControlView = [[ZFADControlView alloc] init];
  99. @zf_weakify(self)
  100. _adControlView.skipCallback = ^{
  101. @zf_strongify(self)
  102. self.player.controlView = self.controlView;
  103. self.player.currentPlayerManager = self.playerManager;
  104. self.playerManager.shouldAutoPlay = YES;
  105. [self.player.currentPlayerManager play];
  106. [self.controlView showTitle:@"iPhone X" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
  107. };
  108. _adControlView.fullScreenCallback = ^{
  109. @zf_strongify(self)
  110. if (self.player.isFullScreen) {
  111. [self.player enterFullScreen:NO animated:YES];
  112. } else {
  113. [self.player enterFullScreen:YES animated:YES];
  114. }
  115. };
  116. }
  117. return _adControlView;
  118. }
  119. - (UIImageView *)containerView {
  120. if (!_containerView) {
  121. _containerView = [UIImageView new];
  122. [_containerView setImageWithURLString:kVideoCover placeholder:[ZFUtilities imageWithColor:[UIColor colorWithRed:220/255.0 green:220/255.0 blue:220/255.0 alpha:1] size:CGSizeMake(1, 1)]];
  123. }
  124. return _containerView;
  125. }
  126. @end
  127. #endif