QGVAPWrapView.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // UIView+VAP.m
  2. // Tencent is pleased to support the open source community by making vap available.
  3. //
  4. // Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  5. //
  6. // Licensed under the MIT License (the "License"); you may not use this file except in
  7. // compliance with the License. You may obtain a copy of the License at
  8. //
  9. // http://opensource.org/licenses/MIT
  10. //
  11. // Unless required by applicable law or agreed to in writing, software distributed under the License is
  12. // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  13. // either express or implied. See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #import "QGVAPWrapView.h"
  16. #import "QGVAPConfigModel.h"
  17. @interface QGVAPWrapView()<VAPWrapViewDelegate, HWDMP4PlayDelegate>
  18. @property (nonatomic, weak) id<VAPWrapViewDelegate> delegate;
  19. @property (nonatomic, strong) VAPView *vapView;
  20. @end
  21. @implementation QGVAPWrapView
  22. - (instancetype)init {
  23. if (self = [super init]) {
  24. [self commonInit];
  25. }
  26. return self;
  27. }
  28. - (instancetype)initWithFrame:(CGRect)frame {
  29. if (self = [super initWithFrame:frame]) {
  30. [self commonInit];
  31. }
  32. return self;
  33. }
  34. - (void)commonInit {
  35. _autoDestoryAfterFinish = YES;
  36. }
  37. // 因为播放停止后可能移除VAPView,这里需要加回来
  38. - (void)initVAPViewIfNeed {
  39. if (!_vapView) {
  40. _vapView = [[VAPView alloc] initWithFrame:self.bounds];
  41. [self addSubview:_vapView];
  42. }
  43. }
  44. - (void)playHWDMP4:(NSString *)filePath
  45. repeatCount:(NSInteger)repeatCount
  46. delegate:(id<VAPWrapViewDelegate>)delegate {
  47. self.delegate = delegate;
  48. [self initVAPViewIfNeed];
  49. [self.vapView playHWDMP4:filePath repeatCount:repeatCount delegate:self];
  50. }
  51. - (void)stopHWDMP4 {
  52. [self.vapView stopHWDMP4];
  53. }
  54. - (void)pauseHWDMP4 {
  55. [self.vapView pauseHWDMP4];
  56. }
  57. - (void)resumeHWDMP4 {
  58. [self.vapView resumeHWDMP4];
  59. }
  60. - (void)addVapGesture:(UIGestureRecognizer *)gestureRecognizer callback:(VAPGestureEventBlock)handler {
  61. [self initVAPViewIfNeed];
  62. [self.vapView addVapGesture:gestureRecognizer callback:handler];
  63. }
  64. - (void)addVapTapGesture:(VAPGestureEventBlock)handler {
  65. [self initVAPViewIfNeed];
  66. [self.vapView addVapTapGesture:handler];
  67. }
  68. #pragma mark - UIView
  69. // 自身不响应,仅子视图响应。
  70. - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
  71. if (!self.isUserInteractionEnabled || self.isHidden || self.alpha < 0.01) {
  72. return nil;
  73. }
  74. if ([self pointInside:point withEvent:event]) {
  75. for (UIView *subview in [self.subviews reverseObjectEnumerator]) {
  76. CGPoint convertedPoint = [self convertPoint:point toView:subview];
  77. UIView *hitView = [subview hitTest:convertedPoint withEvent:event];
  78. if (hitView) {
  79. return hitView;
  80. }
  81. }
  82. return nil;
  83. }
  84. return nil;
  85. }
  86. #pragma mark - Private
  87. - (void)p_setupContentModeWithConfig:(QGVAPConfigModel *)config {
  88. CGFloat realWidth = 0.;
  89. CGFloat realHeight = 0.;
  90. CGFloat layoutWidth = self.bounds.size.width;
  91. CGFloat layoutHeight = self.bounds.size.height;
  92. CGFloat layoutRatio = self.bounds.size.width / self.bounds.size.height;
  93. CGFloat videoRatio = config.info.size.width / config.info.size.height;
  94. switch (self.contentMode) {
  95. case QGVAPWrapViewContentModeScaleToFill: {
  96. }
  97. break;
  98. case QGVAPWrapViewContentModeAspectFit: {
  99. if (layoutRatio < videoRatio) {
  100. realWidth = layoutWidth;
  101. realHeight = realWidth / videoRatio;
  102. } else {
  103. realHeight = layoutHeight;
  104. realWidth = videoRatio * realHeight;
  105. }
  106. self.vapView.frame = CGRectMake(0, 0, realWidth, realHeight);
  107. self.vapView.center = self.center;
  108. }
  109. break;;
  110. case QGVAPWrapViewContentModeAspectFill: {
  111. if (layoutRatio > videoRatio) {
  112. realWidth = layoutWidth;
  113. realHeight = realWidth / videoRatio;
  114. } else {
  115. realHeight = layoutHeight;
  116. realWidth = videoRatio * realHeight;
  117. }
  118. self.vapView.frame = CGRectMake(0, 0, realWidth, realHeight);
  119. self.vapView.center = self.center;
  120. }
  121. break;;
  122. default:
  123. break;
  124. }
  125. }
  126. #pragma mark - mp4 hwd delegate
  127. #pragma mark -- 播放流程
  128. - (void)viewDidStartPlayMP4:(VAPView *)container {
  129. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidStartPlayMP4:)]) {
  130. [self.delegate vapWrap_viewDidStartPlayMP4:container];
  131. }
  132. }
  133. - (void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(UIView *)container {
  134. //note:在子线程被调用
  135. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidFinishPlayMP4:view:)]) {
  136. [self.delegate vapWrap_viewDidFinishPlayMP4:totalFrameCount view:container];
  137. }
  138. }
  139. - (void)viewDidPlayMP4AtFrame:(QGMP4AnimatedImageFrame *)frame view:(UIView *)container {
  140. //note:在子线程被调用
  141. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidPlayMP4AtFrame:view:)]) {
  142. [self.delegate vapWrap_viewDidPlayMP4AtFrame:frame view:container];
  143. }
  144. }
  145. - (void)viewDidStopPlayMP4:(NSInteger)lastFrameIndex view:(UIView *)container {
  146. //note:在子线程被调用
  147. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidStopPlayMP4:view:)]) {
  148. [self.delegate vapWrap_viewDidStopPlayMP4:lastFrameIndex view:container];
  149. }
  150. dispatch_async(dispatch_get_main_queue(), ^{
  151. if (self.autoDestoryAfterFinish) {
  152. [self.vapView removeFromSuperview];
  153. self.vapView = nil;
  154. }
  155. });
  156. }
  157. - (BOOL)shouldStartPlayMP4:(VAPView *)container config:(QGVAPConfigModel *)config {
  158. [self p_setupContentModeWithConfig:config];
  159. if ([self.delegate respondsToSelector:@selector(vapWrap_viewshouldStartPlayMP4:config:)]) {
  160. return [self.delegate vapWrap_viewshouldStartPlayMP4:container config:config];
  161. }
  162. return YES;
  163. }
  164. - (void)viewDidFailPlayMP4:(NSError *)error {
  165. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidFailPlayMP4:)]) {
  166. [self.delegate vapWrap_viewDidFailPlayMP4:error];
  167. }
  168. }
  169. #pragma mark -- 融合特效的接口 vapx
  170. //provide the content for tags, maybe text or url string ...
  171. - (NSString *)contentForVapTag:(NSString *)tag resource:(QGVAPSourceInfo *)info {
  172. if ([self.delegate respondsToSelector:@selector(vapWrapview_contentForVapTag:resource:)]) {
  173. return [self.delegate vapWrapview_contentForVapTag:tag resource:info];
  174. }
  175. return nil;
  176. }
  177. //provide image for url from tag content
  178. - (void)loadVapImageWithURL:(NSString *)urlStr context:(NSDictionary *)context completion:(VAPImageCompletionBlock)completionBlock {
  179. if ([self.delegate respondsToSelector:@selector(vapWrapView_loadVapImageWithURL:context:completion:)]) {
  180. [self.delegate vapWrapView_loadVapImageWithURL:urlStr context:context completion:completionBlock];
  181. }
  182. }
  183. @end