TZVideoPlayerController.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // TZVideoPlayerController.m
  3. // TZImagePickerController
  4. //
  5. // Created by 谭真 on 16/1/5.
  6. // Copyright © 2016年 谭真. All rights reserved.
  7. //
  8. #import "TZVideoPlayerController.h"
  9. #import <MediaPlayer/MediaPlayer.h>
  10. #import "UIView+Layout.h"
  11. #import "TZImageManager.h"
  12. #import "TZAssetModel.h"
  13. #import "TZAssetCell.h"
  14. #import "TZImagePickerController.h"
  15. #import "TZPhotoPreviewController.h"
  16. @interface TZVideoPlayerController () {
  17. AVPlayer *_player;
  18. AVPlayerLayer *_playerLayer;
  19. UIButton *_playButton;
  20. UIImage *_cover;
  21. UIView *_toolBar;
  22. UIButton *_doneButton;
  23. UIProgressView *_progress;
  24. UIStatusBarStyle _originStatusBarStyle;
  25. }
  26. @property (assign, nonatomic) BOOL needShowStatusBar;
  27. @end
  28. #pragma clang diagnostic push
  29. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  30. @implementation TZVideoPlayerController
  31. - (void)viewDidLoad {
  32. [super viewDidLoad];
  33. self.needShowStatusBar = ![UIApplication sharedApplication].statusBarHidden;
  34. self.view.backgroundColor = [UIColor blackColor];
  35. TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
  36. if (tzImagePickerVc) {
  37. self.navigationItem.title = tzImagePickerVc.previewBtnTitleStr;
  38. }
  39. [self configMoviePlayer];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayerAndShowNaviBar) name:UIApplicationWillResignActiveNotification object:nil];
  41. }
  42. - (void)viewWillAppear:(BOOL)animated {
  43. [super viewWillAppear:animated];
  44. _originStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
  45. [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
  46. }
  47. - (void)viewWillDisappear:(BOOL)animated {
  48. [super viewWillDisappear:animated];
  49. [UIApplication sharedApplication].statusBarStyle = _originStatusBarStyle;
  50. }
  51. - (void)configMoviePlayer {
  52. [[TZImageManager manager] getPhotoWithAsset:self.currentCell.model.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
  53. if (!isDegraded && photo) {
  54. self->_cover = photo;
  55. self->_doneButton.enabled = YES;
  56. }
  57. }];
  58. [[TZImageManager manager] getVideoWithAsset:self.currentCell.model.asset completion:^(AVPlayerItem *playerItem, NSDictionary *info) {
  59. dispatch_async(dispatch_get_main_queue(), ^{
  60. self->_player = [AVPlayer playerWithPlayerItem:playerItem];
  61. self->_playerLayer = [AVPlayerLayer playerLayerWithPlayer:self->_player];
  62. self->_playerLayer.frame = self.view.bounds;
  63. [self.view.layer addSublayer:self->_playerLayer];
  64. [self addProgressObserver];
  65. [self configPlayButton];
  66. [self configBottomToolBar];
  67. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayerAndShowNaviBar) name:AVPlayerItemDidPlayToEndTimeNotification object:self->_player.currentItem];
  68. });
  69. }];
  70. }
  71. /// Show progress,do it next time / 给播放器添加进度更新,下次加上
  72. - (void)addProgressObserver{
  73. AVPlayerItem *playerItem = _player.currentItem;
  74. UIProgressView *progress = _progress;
  75. [_player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
  76. float current = CMTimeGetSeconds(time);
  77. float total = CMTimeGetSeconds([playerItem duration]);
  78. if (current) {
  79. [progress setProgress:(current/total) animated:YES];
  80. }
  81. }];
  82. }
  83. - (void)configPlayButton {
  84. _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
  85. [_playButton setImage:[UIImage tz_imageNamedFromMyBundle:@"MMVideoPreviewPlay"] forState:UIControlStateNormal];
  86. [_playButton setImage:[UIImage tz_imageNamedFromMyBundle:@"MMVideoPreviewPlayHL"] forState:UIControlStateHighlighted];
  87. [_playButton addTarget:self action:@selector(playButtonClick) forControlEvents:UIControlEventTouchUpInside];
  88. [self.view addSubview:_playButton];
  89. }
  90. - (void)configBottomToolBar {
  91. _toolBar = [[UIView alloc] initWithFrame:CGRectZero];
  92. CGFloat rgb = 34 / 255.0;
  93. _toolBar.backgroundColor = [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:0.7];
  94. _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  95. _doneButton.titleLabel.font = [UIFont systemFontOfSize:16];
  96. if (!_cover) {
  97. _doneButton.enabled = NO;
  98. }
  99. [_doneButton addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
  100. TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
  101. if (tzImagePickerVc) {
  102. [_doneButton setTitle:tzImagePickerVc.doneBtnTitleStr forState:UIControlStateNormal];
  103. [_doneButton setTitleColor:tzImagePickerVc.oKButtonTitleColorNormal forState:UIControlStateNormal];
  104. } else {
  105. [_doneButton setTitle:[NSBundle tz_localizedStringForKey:@"Done"] forState:UIControlStateNormal];
  106. [_doneButton setTitleColor:[UIColor colorWithRed:(83/255.0) green:(179/255.0) blue:(17/255.0) alpha:1.0] forState:UIControlStateNormal];
  107. }
  108. [_doneButton setTitleColor:tzImagePickerVc.oKButtonTitleColorDisabled forState:UIControlStateDisabled];
  109. [_toolBar addSubview:_doneButton];
  110. [self.view addSubview:_toolBar];
  111. if (tzImagePickerVc.videoPreviewPageUIConfigBlock) {
  112. tzImagePickerVc.videoPreviewPageUIConfigBlock(_playButton, _toolBar, _doneButton);
  113. }
  114. }
  115. - (UIStatusBarStyle)preferredStatusBarStyle {
  116. TZImagePickerController *tzImagePicker = (TZImagePickerController *)self.navigationController;
  117. if (tzImagePicker && [tzImagePicker isKindOfClass:[TZImagePickerController class]]) {
  118. return tzImagePicker.statusBarStyle;
  119. }
  120. return [super preferredStatusBarStyle];
  121. }
  122. #pragma mark - Layout
  123. - (void)viewDidLayoutSubviews {
  124. [super viewDidLayoutSubviews];
  125. CGFloat statusBarHeight = [TZCommonTools tz_statusBarHeight];
  126. CGFloat statusBarAndNaviBarHeight = statusBarHeight + self.navigationController.navigationBar.tz_height;
  127. _playerLayer.frame = self.view.bounds;
  128. CGFloat toolBarHeight = [TZCommonTools tz_isIPhoneX] ? 44 + (83 - 49) : 44;
  129. _toolBar.frame = CGRectMake(0, self.view.tz_height - toolBarHeight, self.view.tz_width, toolBarHeight);
  130. _doneButton.frame = CGRectMake(self.view.tz_width - 44 - 12, 0, 44, 44);
  131. _playButton.frame = CGRectMake(0, statusBarAndNaviBarHeight, self.view.tz_width, self.view.tz_height - statusBarAndNaviBarHeight - toolBarHeight);
  132. TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
  133. if (tzImagePickerVc.videoPreviewPageDidLayoutSubviewsBlock) {
  134. tzImagePickerVc.videoPreviewPageDidLayoutSubviewsBlock(_playButton, _toolBar, _doneButton);
  135. }
  136. }
  137. #pragma mark - Click Event
  138. - (void)playButtonClick {
  139. CMTime currentTime = _player.currentItem.currentTime;
  140. CMTime durationTime = _player.currentItem.duration;
  141. if (_player.rate == 0.0f) {
  142. if (currentTime.value == durationTime.value) [_player.currentItem seekToTime:CMTimeMake(0, 1)];
  143. [_player play];
  144. [self.navigationController setNavigationBarHidden:YES];
  145. _toolBar.hidden = YES;
  146. [_playButton setImage:nil forState:UIControlStateNormal];
  147. [UIApplication sharedApplication].statusBarHidden = YES;
  148. } else {
  149. [self pausePlayerAndShowNaviBar];
  150. }
  151. }
  152. - (void)doneButtonClick {
  153. if (self.navigationController) {
  154. TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
  155. if (imagePickerVc.autoDismiss) {
  156. [self.navigationController dismissViewControllerAnimated:YES completion:^{
  157. [self callDelegateMethod];
  158. }];
  159. } else {
  160. [self callDelegateMethod];
  161. }
  162. } else {
  163. [self dismissViewControllerAnimated:YES completion:^{
  164. [self callDelegateMethod];
  165. }];
  166. }
  167. }
  168. - (void)callDelegateMethod {
  169. [_currentCell toggleCheck:YES];
  170. TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
  171. if ([imagePickerVc.pickerDelegate respondsToSelector:@selector(imagePickerController:didFinishPickingVideo:sourceAssets:)]) {
  172. [imagePickerVc.pickerDelegate imagePickerController:imagePickerVc didFinishPickingVideo:_cover sourceAssets:self.currentCell.model.asset];
  173. }
  174. if (imagePickerVc.didFinishPickingVideoHandle) {
  175. imagePickerVc.didFinishPickingVideoHandle(_cover,self.currentCell.model.asset);
  176. }
  177. if (self.navigationController && self.navigationController.topViewController == self) {
  178. [self.navigationController popViewControllerAnimated:YES];
  179. }
  180. }
  181. #pragma mark - Notification Method
  182. - (void)pausePlayerAndShowNaviBar {
  183. [_player pause];
  184. _toolBar.hidden = NO;
  185. [self.navigationController setNavigationBarHidden:NO];
  186. [_playButton setImage:[UIImage tz_imageNamedFromMyBundle:@"MMVideoPreviewPlay"] forState:UIControlStateNormal];
  187. if (self.needShowStatusBar) {
  188. [UIApplication sharedApplication].statusBarHidden = NO;
  189. }
  190. }
  191. - (void)dealloc {
  192. [[NSNotificationCenter defaultCenter] removeObserver:self];
  193. NSLog(@"#🐣#: release TZVideoPlayerController");
  194. }
  195. #pragma clang diagnostic pop
  196. @end