TCVideoPreview.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. //
  2. // TCVideoPreview.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by xiang zhang on 2017/4/18.
  6. // Copyright © 2017年 tencent. All rights reserved.
  7. //
  8. #import "TCVideoPreview.h"
  9. //#import "UIView+AdditionsX12.h"
  10. #import <AVFoundation/AVFoundation.h>
  11. #undef _MODULE_
  12. #define _MODULE_ "TXVideoPreview"
  13. #define playBtnWidth 42 //34
  14. #define playBtnHeight 42 //46
  15. #define pauseBtnWidth 42 //27
  16. #define pauseBtnHeight 42
  17. #undef _MODULE_
  18. #define _MODULE_ "TXVideoPreview"
  19. @interface TCVideoPreview()
  20. @end
  21. @implementation TCVideoPreview
  22. {
  23. UIButton *_playBtn;
  24. UIImageView *_coverView;
  25. CGFloat _currentTime;
  26. BOOL _videoIsPlay;
  27. BOOL _appInbackground;
  28. }
  29. - (instancetype)initWithFrame:(CGRect)frame coverImage:(UIImage *)image
  30. {
  31. self = [super initWithFrame:frame];
  32. if (self) {
  33. _renderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
  34. [self addSubview:_renderView];
  35. if (image != nil) {
  36. _coverView = [[UIImageView alloc] initWithFrame:_renderView.frame];
  37. _coverView.contentMode = UIViewContentModeScaleAspectFill;//UIViewContentModeScaleAspectFit;
  38. _coverView.image = image;
  39. _coverView.hidden = NO;
  40. [self addSubview:_coverView];
  41. }
  42. _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  43. [self setPlayBtn:_videoIsPlay];
  44. [_playBtn addTarget:self action:@selector(playBtnClick) forControlEvents:UIControlEventTouchUpInside];
  45. [self addSubview:_playBtn];
  46. [[NSNotificationCenter defaultCenter] addObserver:self
  47. selector:@selector(applicationWillEnterForeground:)
  48. name:UIApplicationWillEnterForegroundNotification
  49. object:nil];
  50. [[NSNotificationCenter defaultCenter] addObserver:self
  51. selector:@selector(applicationDidEnterBackground:)
  52. name:UIApplicationDidEnterBackgroundNotification
  53. object:nil];
  54. [[NSNotificationCenter defaultCenter] addObserver:self
  55. selector:@selector(applicationDidBecomeActive:)
  56. name:UIApplicationDidBecomeActiveNotification
  57. object:nil];
  58. [[NSNotificationCenter defaultCenter] addObserver:self
  59. selector:@selector(applicationWillResignActive:)
  60. name:UIApplicationWillResignActiveNotification
  61. object:nil];
  62. [[NSNotificationCenter defaultCenter] addObserver:self
  63. selector:@selector(onAudioSessionEvent:)
  64. name:AVAudioSessionInterruptionNotification
  65. object:nil];
  66. }
  67. return self;
  68. }
  69. - (void)dealloc
  70. {
  71. [[NSNotificationCenter defaultCenter] removeObserver:self];
  72. }
  73. - (void)layoutSubviews
  74. {
  75. [super layoutSubviews];
  76. _renderView.frame = CGRectMake(0, 0, self.width, self.height);
  77. _coverView.frame = _renderView.frame;
  78. if (_videoIsPlay) {
  79. _playBtn.frame = CGRectMake((self.frame.size.width - pauseBtnWidth)/2, (self.frame.size.height - pauseBtnHeight)/2.5 , pauseBtnWidth, pauseBtnHeight);
  80. } else {
  81. _playBtn.frame = CGRectMake((self.frame.size.width - playBtnWidth)/2, (self.frame.size.height - playBtnHeight)/2.5 , playBtnWidth, playBtnHeight);
  82. }
  83. }
  84. - (BOOL)isPlaying
  85. {
  86. return _videoIsPlay;
  87. }
  88. - (void)removeNotification
  89. {
  90. [[NSNotificationCenter defaultCenter] removeObserver:self];
  91. }
  92. - (void)applicationWillEnterForeground:(NSNotification *)noti
  93. {
  94. if (_appInbackground){
  95. if (_delegate && [_delegate respondsToSelector:@selector(onVideoWillEnterForeground)]) {
  96. [_delegate onVideoWillEnterForeground];
  97. }
  98. _appInbackground = NO;
  99. }
  100. }
  101. - (void)applicationDidEnterBackground:(NSNotification *)noti
  102. {
  103. if (_videoIsPlay) {
  104. [self playBtnClick];
  105. }
  106. if (!_appInbackground) {
  107. if (_delegate && [_delegate respondsToSelector:@selector(onVideoEnterBackground)]) {
  108. [_delegate onVideoEnterBackground];
  109. }
  110. _appInbackground = YES;
  111. }
  112. }
  113. - (void)applicationDidBecomeActive:(NSNotification *)noti
  114. {
  115. if (_appInbackground){
  116. if (_delegate && [_delegate respondsToSelector:@selector(onVideoWillEnterForeground)]) {
  117. [_delegate onVideoWillEnterForeground];
  118. }
  119. _appInbackground = NO;
  120. }
  121. }
  122. - (void)applicationWillResignActive:(NSNotification *)noti
  123. {
  124. if (_videoIsPlay) {
  125. [self playBtnClick];
  126. }
  127. if (!_appInbackground) {
  128. if (_delegate && [_delegate respondsToSelector:@selector(onVideoEnterBackground)]) {
  129. [_delegate onVideoEnterBackground];
  130. }
  131. _appInbackground = YES;
  132. }
  133. }
  134. - (void) onAudioSessionEvent: (NSNotification *) notification
  135. {
  136. NSDictionary *info = notification.userInfo;
  137. AVAudioSessionInterruptionType type = [info[AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
  138. if (type == AVAudioSessionInterruptionTypeBegan) {
  139. if (!_appInbackground) {
  140. if (_delegate && [_delegate respondsToSelector:@selector(onVideoEnterBackground)]) {
  141. [_delegate onVideoEnterBackground];
  142. }
  143. _appInbackground = YES;
  144. }
  145. _coverView.hidden = YES;
  146. if (_videoIsPlay) {
  147. _videoIsPlay = NO;
  148. [self setPlayBtn:_videoIsPlay];
  149. }
  150. }
  151. }
  152. - (void)setPlayBtnHidden:(BOOL)isHidden
  153. {
  154. _playBtn.hidden = isHidden;
  155. }
  156. - (void)playVideo
  157. {
  158. [self playBtnClick];
  159. }
  160. - (void)playBtnClick
  161. {
  162. _coverView.hidden = YES;
  163. if (_videoIsPlay) {
  164. _videoIsPlay = NO;
  165. [self setPlayBtn:_videoIsPlay];
  166. if (_delegate && [_delegate respondsToSelector:@selector(onVideoPause)]) {
  167. [_delegate onVideoPause];
  168. }
  169. }else{
  170. _videoIsPlay = YES;
  171. [self setPlayBtn:_videoIsPlay];
  172. if (_currentTime == 0) {
  173. if (_delegate && [_delegate respondsToSelector:@selector(onVideoPlay)]) {
  174. [_delegate onVideoPlay];
  175. }
  176. }else{
  177. if (_delegate && [_delegate respondsToSelector:@selector(onVideoResume)]) {
  178. [_delegate onVideoResume];
  179. }
  180. }
  181. }
  182. }
  183. -(void) setPlayBtn:(BOOL)videoIsPlay
  184. {
  185. if (videoIsPlay) {//pause_ugc_edit
  186. //暂停
  187. [_playBtn setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
  188. _coverView.hidden = YES;
  189. }else{
  190. //play_ugc_edit
  191. //播放
  192. [_playBtn setImage:[UIImage imageNamed:@"ask_play"] forState:UIControlStateNormal];
  193. }
  194. _videoIsPlay = videoIsPlay;
  195. }
  196. -(void) onPreviewProgress:(CGFloat)time
  197. {
  198. _currentTime = time;
  199. if (_delegate && [_delegate respondsToSelector:@selector(onVideoPlayProgress:)]) {
  200. [_delegate onVideoPlayProgress:time];
  201. }
  202. }
  203. -(void) onPreviewFinished
  204. {
  205. if (_delegate && [_delegate respondsToSelector:@selector(onVideoPlayFinished)]) {
  206. [_delegate onVideoPlayFinished];
  207. }
  208. }
  209. @end