ViewController.m 5.8 KB

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