QGVAPWrapView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. self.vapView = [[VAPView alloc] initWithFrame:self.bounds];
  36. [self addSubview:self.vapView];
  37. }
  38. - (void)vapWrapView_playHWDMP4:(NSString *)filePath
  39. repeatCount:(NSInteger)repeatCount
  40. delegate:(id<VAPWrapViewDelegate>)delegate {
  41. self.delegate = delegate;
  42. [self.vapView playHWDMP4:filePath repeatCount:repeatCount delegate:self];
  43. }
  44. #pragma mark - Private
  45. - (void)p_setupContentModeWithConfig:(QGVAPConfigModel *)config {
  46. CGFloat realWidth = 0.;
  47. CGFloat realHeight = 0.;
  48. CGFloat layoutWidth = self.bounds.size.width;
  49. CGFloat layoutHeight = self.bounds.size.height;
  50. CGFloat layoutRatio = self.bounds.size.width / self.bounds.size.height;
  51. CGFloat videoRatio = config.info.size.width / config.info.size.height;
  52. switch (self.contentMode) {
  53. case QGVAPWrapViewContentModeScaleToFill:
  54. {
  55. }
  56. break;
  57. case QGVAPWrapViewContentModeAspectFit:
  58. {
  59. if (layoutRatio < videoRatio) {
  60. realWidth = layoutWidth;
  61. realHeight = realWidth / videoRatio;
  62. } else {
  63. realHeight = layoutHeight;
  64. realWidth = videoRatio * realHeight;
  65. }
  66. self.vapView.frame = CGRectMake(0, 0, realWidth, realHeight);
  67. self.vapView.center = self.center;
  68. }
  69. break;;
  70. case QGVAPWrapViewContentModeAspectFill:
  71. {
  72. if (layoutRatio > videoRatio) {
  73. realWidth = layoutWidth;
  74. realHeight = realWidth / videoRatio;
  75. } else {
  76. realHeight = layoutHeight;
  77. realWidth = videoRatio * realHeight;
  78. }
  79. self.vapView.frame = CGRectMake(0, 0, realWidth, realHeight);
  80. self.vapView.center = self.center;
  81. }
  82. break;;
  83. default:
  84. break;
  85. }
  86. }
  87. #pragma mark - mp4 hwd delegate
  88. #pragma mark -- 播放流程
  89. - (void)viewDidStartPlayMP4:(VAPView *)container {
  90. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidStartPlayMP4:)]) {
  91. [self.delegate vapWrap_viewDidStartPlayMP4:container];
  92. }
  93. }
  94. - (void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(UIView *)container {
  95. //note:在子线程被调用
  96. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidFinishPlayMP4:view:)]) {
  97. [self.delegate vapWrap_viewDidFinishPlayMP4:totalFrameCount view:container];
  98. }
  99. }
  100. - (void)viewDidPlayMP4AtFrame:(QGMP4AnimatedImageFrame *)frame view:(UIView *)container {
  101. //note:在子线程被调用
  102. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidPlayMP4AtFrame:view:)]) {
  103. [self.delegate vapWrap_viewDidPlayMP4AtFrame:frame view:container];
  104. }
  105. }
  106. - (void)viewDidStopPlayMP4:(NSInteger)lastFrameIndex view:(UIView *)container {
  107. //note:在子线程被调用
  108. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidStopPlayMP4:view:)]) {
  109. [self.delegate vapWrap_viewDidStopPlayMP4:lastFrameIndex view:container];
  110. }
  111. }
  112. - (BOOL)shouldStartPlayMP4:(VAPView *)container config:(QGVAPConfigModel *)config {
  113. [self p_setupContentModeWithConfig:config];
  114. if ([self.delegate respondsToSelector:@selector(vapWrap_viewshouldStartPlayMP4:config:)]) {
  115. return [self.delegate vapWrap_viewshouldStartPlayMP4:container config:config];
  116. }
  117. return YES;
  118. }
  119. - (void)viewDidFailPlayMP4:(NSError *)error {
  120. if ([self.delegate respondsToSelector:@selector(vapWrap_viewDidFailPlayMP4:)]) {
  121. [self.delegate vapWrap_viewDidFailPlayMP4:error];
  122. }
  123. }
  124. #pragma mark -- 融合特效的接口 vapx
  125. //provide the content for tags, maybe text or url string ...
  126. - (NSString *)contentForVapTag:(NSString *)tag resource:(QGVAPSourceInfo *)info {
  127. if ([self.delegate respondsToSelector:@selector(vapWrapview_contentForVapTag:resource:)]) {
  128. return [self.delegate vapWrapview_contentForVapTag:tag resource:info];
  129. }
  130. return nil;
  131. }
  132. //provide image for url from tag content
  133. - (void)loadVapImageWithURL:(NSString *)urlStr context:(NSDictionary *)context completion:(VAPImageCompletionBlock)completionBlock {
  134. if ([self.delegate respondsToSelector:@selector(vapWrapView_loadVapImageWithURL:context:completion:)]) {
  135. [self.delegate vapWrapView_loadVapImageWithURL:urlStr context:context completion:completionBlock];
  136. }
  137. }
  138. @end