UGCKitPlayerView.m 7.7 KB

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