TZVideoPlayerController.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "TZImagePickerController.h"
  14. #import "TZPhotoPreviewController.h"
  15. @interface TZVideoPlayerController () {
  16. AVPlayer *_player;
  17. UIButton *_playButton;
  18. UIImage *_cover;
  19. UIView *_toolBar;
  20. UIButton *_doneButton;
  21. UIProgressView *_progress;
  22. UIStatusBarStyle _originStatusBarStyle;
  23. }
  24. @end
  25. @implementation TZVideoPlayerController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. self.view.backgroundColor = [UIColor blackColor];
  29. TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
  30. if (tzImagePickerVc) {
  31. self.navigationItem.title = tzImagePickerVc.previewBtnTitleStr;
  32. }
  33. [self configMoviePlayer];
  34. }
  35. - (void)viewWillAppear:(BOOL)animated {
  36. [super viewWillAppear:animated];
  37. _originStatusBarStyle = [UIApplication sharedApplication].statusBarStyle;
  38. [UIApplication sharedApplication].statusBarStyle = iOS7Later ? UIStatusBarStyleLightContent : UIStatusBarStyleBlackOpaque;
  39. }
  40. - (void)viewWillDisappear:(BOOL)animated {
  41. [super viewWillDisappear:animated];
  42. [UIApplication sharedApplication].statusBarStyle = _originStatusBarStyle;
  43. }
  44. - (void)configMoviePlayer {
  45. [[TZImageManager manager] getPhotoWithAsset:_model.asset completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
  46. _cover = photo;
  47. }];
  48. [[TZImageManager manager] getVideoWithAsset:_model.asset completion:^(AVPlayerItem *playerItem, NSDictionary *info) {
  49. dispatch_async(dispatch_get_main_queue(), ^{
  50. _player = [AVPlayer playerWithPlayerItem:playerItem];
  51. AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:_player];
  52. playerLayer.frame = self.view.bounds;
  53. [self.view.layer addSublayer:playerLayer];
  54. [self addProgressObserver];
  55. [self configPlayButton];
  56. [self configBottomToolBar];
  57. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pausePlayerAndShowNaviBar) name:AVPlayerItemDidPlayToEndTimeNotification object:_player.currentItem];
  58. });
  59. }];
  60. }
  61. /// Show progress,do it next time / 给播放器添加进度更新,下次加上
  62. - (void)addProgressObserver{
  63. AVPlayerItem *playerItem = _player.currentItem;
  64. UIProgressView *progress = _progress;
  65. [_player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
  66. float current = CMTimeGetSeconds(time);
  67. float total = CMTimeGetSeconds([playerItem duration]);
  68. if (current) {
  69. [progress setProgress:(current/total) animated:YES];
  70. }
  71. }];
  72. }
  73. - (void)configPlayButton {
  74. _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
  75. _playButton.frame = CGRectMake(0, 64, self.view.tz_width, self.view.tz_height - 64 - 44);
  76. [_playButton setImage:[UIImage imageNamedFromMyBundle:@"MMVideoPreviewPlay.png"] forState:UIControlStateNormal];
  77. [_playButton setImage:[UIImage imageNamedFromMyBundle:@"MMVideoPreviewPlayHL.png"] forState:UIControlStateHighlighted];
  78. [_playButton addTarget:self action:@selector(playButtonClick) forControlEvents:UIControlEventTouchUpInside];
  79. [self.view addSubview:_playButton];
  80. }
  81. - (void)configBottomToolBar {
  82. _toolBar = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.tz_height - 44, self.view.tz_width, 44)];
  83. // CGFloat rgb = 34 / 255.0;
  84. _toolBar.backgroundColor = kWhiteColor;
  85. // [UIColor colorWithRed:rgb green:rgb blue:rgb alpha:0.7];
  86. _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
  87. _doneButton.frame = CGRectMake(self.view.tz_width - 44 - 12, 0, 44, 44);
  88. _doneButton.titleLabel.font = [UIFont systemFontOfSize:16];
  89. [_doneButton addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
  90. TZImagePickerController *tzImagePickerVc = (TZImagePickerController *)self.navigationController;
  91. if (tzImagePickerVc) {
  92. [_doneButton setTitle:tzImagePickerVc.doneBtnTitleStr forState:UIControlStateNormal];
  93. // [_doneButton setTitleColor:tzImagePickerVc.oKButtonTitleColorNormal forState:UIControlStateNormal];
  94. } else {
  95. [_doneButton setTitle:[NSBundle tz_localizedStringForKey:@"Done"] forState:UIControlStateNormal];
  96. // [_doneButton setTitleColor:[UIColor colorWithRed:(83/255.0) green:(179/255.0) blue:(17/255.0) alpha:1.0] forState:UIControlStateNormal];
  97. }
  98. [_doneButton setTitleColor:tzImagePickerVc.oKButtonTitleColorNormal forState:UIControlStateNormal];
  99. [_toolBar addSubview:_doneButton];
  100. [self.view addSubview:_toolBar];
  101. }
  102. #pragma mark - Click Event
  103. - (void)playButtonClick {
  104. CMTime currentTime = _player.currentItem.currentTime;
  105. CMTime durationTime = _player.currentItem.duration;
  106. if (_player.rate == 0.0f) {
  107. if (currentTime.value == durationTime.value) [_player.currentItem seekToTime:CMTimeMake(0, 1)];
  108. [_player play];
  109. [self.navigationController setNavigationBarHidden:YES];
  110. _toolBar.hidden = YES;
  111. [_playButton setImage:nil forState:UIControlStateNormal];
  112. if (iOS7Later) [UIApplication sharedApplication].statusBarHidden = YES;
  113. } else {
  114. [self pausePlayerAndShowNaviBar];
  115. }
  116. }
  117. - (void)doneButtonClick {
  118. TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
  119. if (self.navigationController) {
  120. if (imagePickerVc.autoDismiss) {
  121. [self.navigationController dismissViewControllerAnimated:YES completion:^{
  122. [self callDelegateMethod];
  123. }];
  124. }
  125. } else {
  126. [self dismissViewControllerAnimated:YES completion:^{
  127. [self callDelegateMethod];
  128. }];
  129. }
  130. }
  131. - (void)callDelegateMethod {
  132. TZImagePickerController *imagePickerVc = (TZImagePickerController *)self.navigationController;
  133. if ([imagePickerVc.pickerDelegate respondsToSelector:@selector(imagePickerController:didFinishPickingVideo:sourceAssets:)]) {
  134. [imagePickerVc.pickerDelegate imagePickerController:imagePickerVc didFinishPickingVideo:_cover sourceAssets:_model.asset];
  135. }
  136. if (imagePickerVc.didFinishPickingVideoHandle) {
  137. imagePickerVc.didFinishPickingVideoHandle(_cover,_model.asset);
  138. }
  139. }
  140. #pragma mark - Notification Method
  141. - (void)pausePlayerAndShowNaviBar {
  142. [_player pause];
  143. _toolBar.hidden = NO;
  144. [self.navigationController setNavigationBarHidden:NO];
  145. [_playButton setImage:[UIImage imageNamedFromMyBundle:@"MMVideoPreviewPlay.png"] forState:UIControlStateNormal];
  146. if (iOS7Later) [UIApplication sharedApplication].statusBarHidden = NO;
  147. }
  148. - (void)dealloc {
  149. [[NSNotificationCenter defaultCenter] removeObserver:self];
  150. }
  151. @end