ViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // ViewController.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 "ViewController.h"
  16. #import "UIView+VAP.h"
  17. #import "QGVAPWrapView.h"
  18. @interface ViewController () <HWDMP4PlayDelegate, VAPWrapViewDelegate>
  19. @property (nonatomic, strong) UIButton *vapButton;
  20. @property (nonatomic, strong) UIButton *vapxButton;
  21. @property (nonatomic, strong) UIButton *vapWrapViewButton;
  22. @property (nonatomic, strong) VAPView *vapView;
  23. @end
  24. @implementation ViewController
  25. //日志接口
  26. void qg_VAP_Logger_handler(VAPLogLevel level, const char* file, int line, const char* func, NSString *module, NSString *format, ...) {
  27. if (format.UTF8String == nil) {
  28. NSLog(@"log包含非utf-8字符");
  29. return;
  30. }
  31. if (level > VAPLogLevelDebug) {
  32. va_list argList;
  33. va_start(argList, format);
  34. NSString* message = [[NSString alloc] initWithFormat:format arguments:argList];
  35. file = [NSString stringWithUTF8String:file].lastPathComponent.UTF8String;
  36. NSLog(@"<%@> %s(%@):%s [%@] - %@",@(level), file, @(line), func, module, message);
  37. va_end(argList);
  38. }
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. //日志
  43. [UIView registerHWDLog:qg_VAP_Logger_handler];
  44. //vap-经典效果
  45. _vapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, CGRectGetWidth(self.view.frame), 90)];
  46. _vapButton.backgroundColor = [UIColor lightGrayColor];
  47. [_vapButton setTitle:@"电竞方案" forState:UIControlStateNormal];
  48. [_vapButton addTarget:self action:@selector(playVap) forControlEvents:UIControlEventTouchUpInside];
  49. [self.view addSubview:_vapButton];
  50. //vapx-融合效果
  51. _vapxButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_vapButton.frame)+60, CGRectGetWidth(self.view.frame), 90)];
  52. _vapxButton.backgroundColor = [UIColor lightGrayColor];
  53. [_vapxButton setTitle:@"融合特效" forState:UIControlStateNormal];
  54. [_vapxButton addTarget:self action:@selector(playVapx) forControlEvents:UIControlEventTouchUpInside];
  55. [self.view addSubview:_vapxButton];
  56. //使用WrapView,支持ContentMode
  57. _vapWrapViewButton = [[UIButton alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_vapxButton.frame)+60, CGRectGetWidth(self.view.frame), 90)];
  58. _vapWrapViewButton.backgroundColor = [UIColor lightGrayColor];
  59. [_vapWrapViewButton setTitle:@"WrapView-ContentMode" forState:UIControlStateNormal];
  60. [_vapWrapViewButton addTarget:self action:@selector(playVapWithWrapView) forControlEvents:UIControlEventTouchUpInside];
  61. [self.view addSubview:_vapWrapViewButton];
  62. }
  63. #pragma mark - 各种类型的播放
  64. - (void)playVap {
  65. VAPView *mp4View = [[VAPView alloc] initWithFrame:CGRectMake(0, 0, 752/2, 752/2)];
  66. //默认使用metal渲染,使用OpenGL请打开下面这个开关
  67. //mp4View.renderByOpenGL = YES;
  68. mp4View.center = self.view.center;
  69. [self.view addSubview:mp4View];
  70. mp4View.userInteractionEnabled = YES;
  71. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageviewTap:)];
  72. [mp4View addGestureRecognizer:tap];
  73. NSString *resPath = [NSString stringWithFormat:@"%@/Resource/demo.mp4", [[NSBundle mainBundle] resourcePath]];
  74. //单纯播放的接口
  75. //[mp4View playHWDMp4:resPath];
  76. //指定素材混合模式,重复播放次数,delegate的接口
  77. [mp4View playHWDMP4:resPath repeatCount:-1 delegate:self];
  78. }
  79. //vap动画
  80. - (void)playVapx {
  81. NSString *mp4Path = [NSString stringWithFormat:@"%@/Resource/vap.mp4", [[NSBundle mainBundle] resourcePath]];
  82. VAPView *mp4View = [[VAPView alloc] initWithFrame:self.view.bounds];
  83. [self.view addSubview:mp4View];
  84. mp4View.center = self.view.center;
  85. mp4View.userInteractionEnabled = YES;
  86. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageviewTap:)];
  87. [mp4View addGestureRecognizer:tap];
  88. [mp4View playHWDMP4:mp4Path repeatCount:-1 delegate:self];
  89. }
  90. /// 使用WrapView,支持ContentMode
  91. - (void)playVapWithWrapView {
  92. QGVAPWrapView *wrapView = [[QGVAPWrapView alloc] initWithFrame:self.view.bounds];
  93. wrapView.center = self.view.center;
  94. wrapView.contentMode = QGVAPWrapViewContentModeAspectFit;
  95. wrapView.autoDestoryAfterFinish = YES;
  96. [self.view addSubview:wrapView];
  97. NSString *resPath = [NSString stringWithFormat:@"%@/Resource/demo.mp4", [[NSBundle mainBundle] resourcePath]];
  98. [wrapView vapWrapView_playHWDMP4:resPath repeatCount:-1 delegate:self];
  99. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onImageviewTap:)];
  100. [wrapView vapWrapView_addVapGesture:tap callback:^(UIGestureRecognizer *gestureRecognizer, BOOL insideSource, QGVAPSourceDisplayItem *source) {
  101. }];
  102. }
  103. #pragma mark - mp4 hwd delegate
  104. #pragma mark -- 播放流程
  105. - (void)viewDidStartPlayMP4:(VAPView *)container {
  106. }
  107. - (void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(UIView *)container {
  108. //note:在子线程被调用
  109. }
  110. - (void)viewDidPlayMP4AtFrame:(QGMP4AnimatedImageFrame *)frame view:(UIView *)container {
  111. //note:在子线程被调用
  112. }
  113. - (void)viewDidStopPlayMP4:(NSInteger)lastFrameIndex view:(UIView *)container {
  114. //note:在子线程被调用
  115. dispatch_async(dispatch_get_main_queue(), ^{
  116. [container removeFromSuperview];
  117. });
  118. }
  119. - (BOOL)shouldStartPlayMP4:(VAPView *)container config:(QGVAPConfigModel *)config {
  120. return YES;
  121. }
  122. - (void)viewDidFailPlayMP4:(NSError *)error {
  123. NSLog(@"%@", error.userInfo);
  124. }
  125. #pragma mark -- 融合特效的接口 vapx
  126. //provide the content for tags, maybe text or url string ...
  127. - (NSString *)contentForVapTag:(NSString *)tag resource:(QGVAPSourceInfo *)info {
  128. NSDictionary *extraInfo = @{@"[sImg1]" : @"http://shp.qlogo.cn/pghead/Q3auHgzwzM6GuU0Y6q6sKHzq3MjY1aGibIzR4xrJc1VY/60",
  129. @"[textAnchor]" : @"我是主播名",
  130. @"[textUser]" : @"我是用户名😂😂",};
  131. return extraInfo[tag];
  132. }
  133. //provide image for url from tag content
  134. - (void)loadVapImageWithURL:(NSString *)urlStr context:(NSDictionary *)context completion:(VAPImageCompletionBlock)completionBlock {
  135. //call completionBlock as you get the image, both sync or asyn are ok.
  136. //usually we'd like to make a net request
  137. dispatch_async(dispatch_get_main_queue(), ^{
  138. UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@/Resource/qq.png", [[NSBundle mainBundle] resourcePath]]];
  139. //let's say we've got result here
  140. completionBlock(image, nil, urlStr);
  141. });
  142. }
  143. #pragma mark - gesture
  144. - (void)onImageviewTap:(UIGestureRecognizer *)ges {
  145. [ges.view removeFromSuperview];
  146. }
  147. #pragma mark - WrapViewDelegate
  148. //provide the content for tags, maybe text or url string ...
  149. - (NSString *)vapWrapview_contentForVapTag:(NSString *)tag resource:(QGVAPSourceInfo *)info {
  150. return nil;
  151. }
  152. //provide image for url from tag content
  153. - (void)vapWrapView_loadVapImageWithURL:(NSString *)urlStr context:(NSDictionary *)context completion:(VAPImageCompletionBlock)completionBlock {
  154. }
  155. @end