QGVAPWrapView.m 5.6 KB

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