YOUPAIUCIMVideoVC.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // YOUPAIUCIMVideoVC.m
  3. // wolfman
  4. //
  5. // Created by 张灿 on 2017/6/5.
  6. // Copyright © 2017年 shareSmile. All rights reserved.
  7. //
  8. #import "YOUPAIUCIMVideoVC.h"
  9. @interface YOUPAIUCIMVideoVC ()
  10. @property (nonatomic,strong) NIMVideoObject *videoObject;
  11. @end
  12. @implementation YOUPAIUCIMVideoVC
  13. @synthesize moviePlayer = _moviePlayer;
  14. - (instancetype)initWithVideoObject:(NIMVideoObject *)videoObject{
  15. self = [super initWithNibName:nil bundle:nil];
  16. if (self) {
  17. _videoObject = videoObject;
  18. }
  19. return self;
  20. }
  21. - (void)dealloc{
  22. [[NSNotificationCenter defaultCenter] removeObserver:self];
  23. [[NIMSDK sharedSDK].resourceManager cancelTask:_videoObject.path];
  24. }
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.edgesForExtendedLayout = UIRectEdgeAll;
  28. self.navigationItem.title = @"视频";
  29. if ([[NSFileManager defaultManager] fileExistsAtPath:self.videoObject.path]) {
  30. [self startPlay];
  31. }else{
  32. __weak typeof(self) wself = self;
  33. [self downLoadVideo:^(NSError *error) {
  34. if (!error) {
  35. [wself startPlay];
  36. }else{
  37. [wself.view makeToast:@"下载失败,请检查网络"
  38. duration:2
  39. position:CSToastPositionCenter];
  40. }
  41. }];
  42. }
  43. }
  44. - (void)viewWillDisappear:(BOOL)animated
  45. {
  46. [super viewWillDisappear: animated];
  47. [SVProgressHUD dismiss];
  48. if (![[self.navigationController viewControllers] containsObject: self])
  49. {
  50. [self topStatusUIHidden:NO];
  51. }
  52. }
  53. - (void)viewDidDisappear:(BOOL)animated{
  54. [super viewDidDisappear:animated];
  55. [self.moviePlayer stop];
  56. }
  57. - (void)viewWillAppear:(BOOL)animated{
  58. [super viewWillAppear: animated];
  59. if (_moviePlayer.playbackState == MPMoviePlaybackStatePlaying) {//不要调用.get方法,会过早的初始化播放器
  60. [self topStatusUIHidden:YES];
  61. }else{
  62. [self topStatusUIHidden:NO];
  63. }
  64. }
  65. - (UIStatusBarStyle)preferredStatusBarStyle{
  66. return UIStatusBarStyleLightContent;
  67. }
  68. - (void)downLoadVideo:(void(^)(NSError *error))handler{
  69. [SVProgressHUD show];
  70. __weak typeof(self) wself = self;
  71. [[NIMSDK sharedSDK].resourceManager download:self.videoObject.url filepath:self.videoObject.path progress:^(float progress) {
  72. [SVProgressHUD showProgress:progress];
  73. } completion:^(NSError *error) {
  74. if (wself) {
  75. [SVProgressHUD dismiss];
  76. if (handler) {
  77. handler(error);
  78. }
  79. }
  80. }];
  81. }
  82. - (void)startPlay{
  83. self.moviePlayer.view.frame = self.view.bounds;
  84. self.moviePlayer.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  85. [self.moviePlayer play];
  86. [self.view addSubview:self.moviePlayer.view];
  87. [[NSNotificationCenter defaultCenter] addObserver:self
  88. selector:@selector(moviePlaybackComplete:)
  89. name:MPMoviePlayerPlaybackDidFinishNotification
  90. object:self.moviePlayer];
  91. [[NSNotificationCenter defaultCenter] addObserver:self
  92. selector:@selector(moviePlayStateChanged:)
  93. name:MPMoviePlayerPlaybackStateDidChangeNotification
  94. object:self.moviePlayer];
  95. CGRect bounds = self.moviePlayer.view.bounds;
  96. CGRect tapViewFrame = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
  97. UIView *tapView = [[UIView alloc]initWithFrame:tapViewFrame];
  98. [tapView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
  99. tapView.backgroundColor = [UIColor clearColor];
  100. [self.moviePlayer.view addSubview:tapView];
  101. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
  102. [tapView addGestureRecognizer:tap];
  103. }
  104. - (void)moviePlaybackComplete: (NSNotification *)aNotification
  105. {
  106. if (self.moviePlayer == aNotification.object)
  107. {
  108. [self topStatusUIHidden:NO];
  109. NSDictionary *notificationUserInfo = [aNotification userInfo];
  110. NSNumber *resultValue = [notificationUserInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
  111. MPMovieFinishReason reason = [resultValue intValue];
  112. if (reason == MPMovieFinishReasonPlaybackError)
  113. {
  114. NSError *mediaPlayerError = [notificationUserInfo objectForKey:@"error"];
  115. NSString *errorTip = [NSString stringWithFormat:@"播放失败: %@", [mediaPlayerError localizedDescription]];
  116. [self.view makeToast:errorTip
  117. duration:2
  118. position:CSToastPositionCenter];
  119. }
  120. }
  121. }
  122. - (void)moviePlayStateChanged: (NSNotification *)aNotification
  123. {
  124. if (self.moviePlayer == aNotification.object)
  125. {
  126. switch (self.moviePlayer.playbackState)
  127. {
  128. case MPMoviePlaybackStatePlaying:
  129. [self topStatusUIHidden:YES];
  130. break;
  131. case MPMoviePlaybackStatePaused:
  132. case MPMoviePlaybackStateStopped:
  133. case MPMoviePlaybackStateInterrupted:
  134. [self topStatusUIHidden:NO];
  135. case MPMoviePlaybackStateSeekingBackward:
  136. case MPMoviePlaybackStateSeekingForward:
  137. break;
  138. }
  139. }
  140. }
  141. - (void)topStatusUIHidden:(BOOL)isHidden
  142. {
  143. [[UIApplication sharedApplication] setStatusBarHidden:isHidden];
  144. self.navigationController.navigationBar.hidden = isHidden;
  145. // self.navigationController.interactivePopGestureRecognizer.enabled = isHidden;
  146. }
  147. - (void)onTap: (UIGestureRecognizer *)recognizer
  148. {
  149. switch (self.moviePlayer.playbackState)
  150. {
  151. case MPMoviePlaybackStatePlaying:
  152. [self.moviePlayer pause];
  153. break;
  154. case MPMoviePlaybackStatePaused:
  155. case MPMoviePlaybackStateStopped:
  156. case MPMoviePlaybackStateInterrupted:
  157. [self.moviePlayer play];
  158. break;
  159. default:
  160. break;
  161. }
  162. }
  163. - (MPMoviePlayerController*)moviePlayer{
  164. if (!_moviePlayer) {
  165. _moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:self.videoObject.path]];
  166. _moviePlayer.controlStyle = MPMovieControlStyleNone;
  167. _moviePlayer.scalingMode = MPMovieScalingModeAspectFill;
  168. _moviePlayer.fullscreen = YES;
  169. }
  170. return _moviePlayer;
  171. }
  172. @end