ZFCustomControlViewViewController.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // ZFCustomControlViewViewController.m
  3. // ZFPlayer_Example
  4. //
  5. // Created by 紫枫 on 2019/6/5.
  6. // Copyright © 2019 紫枫. All rights reserved.
  7. //
  8. #import "ZFCustomControlViewViewController.h"
  9. #import "ZFCustomControlView.h"
  10. #import <ZFPlayer/ZFAVPlayerManager.h>
  11. #import <ZFPlayer/ZFIJKPlayerManager.h>
  12. #import <ZFPlayer/ZFPlayerControlView.h>
  13. #import <ZFPlayer/UIView+ZFFrame.h>
  14. #import <ZFPlayer/ZFPlayerConst.h>
  15. #import "UIImageView+ZFCache.h"
  16. #import "ZFUtilities.h"
  17. static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";
  18. @interface ZFCustomControlViewViewController ()
  19. @property (nonatomic, strong) ZFPlayerController *player;
  20. @property (nonatomic, strong) UIImageView *containerView;
  21. @property (nonatomic, strong) ZFCustomControlView *controlView;
  22. @end
  23. @implementation ZFCustomControlViewViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. self.view.backgroundColor = [UIColor whiteColor];
  27. [self.view addSubview:self.containerView];
  28. ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
  29. /// 播放器相关
  30. self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
  31. self.player.controlView = self.controlView;
  32. /// 设置退到后台继续播放
  33. self.player.pauseWhenAppResignActive = NO;
  34. @zf_weakify(self)
  35. /// 播放完成
  36. self.player.playerDidToEnd = ^(id _Nonnull asset) {
  37. @zf_strongify(self)
  38. [self.player.currentPlayerManager replay];
  39. [self.player playTheNext];
  40. if (!self.player.isLastAssetURL) {
  41. NSString *title = [NSString stringWithFormat:@"视频标题%zd",self.player.currentPlayIndex];
  42. [self.controlView showTitle:title coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
  43. } else {
  44. [self.player stop];
  45. }
  46. };
  47. 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"];
  48. [self.controlView showTitle:@"自定义控制层" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeAutomatic];
  49. }
  50. - (void)viewWillAppear:(BOOL)animated {
  51. [super viewWillAppear:animated];
  52. self.player.viewControllerDisappear = NO;
  53. }
  54. - (void)viewWillDisappear:(BOOL)animated {
  55. [super viewWillDisappear:animated];
  56. self.player.viewControllerDisappear = YES;
  57. }
  58. - (void)viewWillLayoutSubviews {
  59. [super viewWillLayoutSubviews];
  60. CGFloat x = 0;
  61. CGFloat y = CGRectGetMaxY(self.navigationController.navigationBar.frame);
  62. CGFloat w = CGRectGetWidth(self.view.frame);
  63. CGFloat h = w*9/16;
  64. self.containerView.frame = CGRectMake(x, y, w, h);
  65. }
  66. - (UIStatusBarStyle)preferredStatusBarStyle {
  67. return UIStatusBarStyleDefault;
  68. }
  69. - (BOOL)prefersStatusBarHidden {
  70. return NO;
  71. }
  72. - (BOOL)shouldAutorotate {
  73. return NO;
  74. }
  75. - (UIInterfaceOrientationMask)supportedInterfaceOrientations {
  76. if (self.player.isFullScreen) {
  77. return UIInterfaceOrientationMaskLandscape;
  78. }
  79. return UIInterfaceOrientationMaskPortrait;
  80. }
  81. - (ZFCustomControlView *)controlView {
  82. if (!_controlView) {
  83. _controlView = [ZFCustomControlView new];
  84. }
  85. return _controlView;
  86. }
  87. - (UIImageView *)containerView {
  88. if (!_containerView) {
  89. _containerView = [UIImageView new];
  90. [_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)]];
  91. }
  92. return _containerView;
  93. }
  94. @end