VideoPreview.m 7.3 KB

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