Files
peko-ios/YuMi/Modules/YMRoom/Features/Boom/RoomBoomExplosionView.m
2024-10-11 16:41:55 +08:00

182 lines
5.1 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// RoomBoomProgressView.m
// YuMi
//
// Created by P on 2024/9/27.
//
#import "RoomBoomExplosionView.h"
#import <QGVAPWrapView.h>
#import "BoomInfoModel.h"
#import "AttachmentModel.h"
#import "RoomBoomManager.h"
#import "XPRoomGiftAnimationParser.h"
@interface RoomBoomExplosionView() <HWDMP4PlayDelegate>
@property (nonatomic, strong) VAPView *vapView;
@property (nonatomic, strong) XPRoomGiftAnimationParser *vapParser;
//@property (nonatomic, strong) Boom632Model *model;
//@property (nonatomic, strong) BoomInfoModel *boomInfo;
@property (nonatomic, assign) NSInteger seq;
@property (nonatomic, copy) NSString *countDownURLString;
@property (nonatomic, copy) NSString *endURLString;
@property (nonatomic, copy) void(^completeDisplay)(void);
@end
@implementation RoomBoomExplosionView
+ (void)display:(UIView *)superView
with:(NSArray *)vapSources
complete:(void(^)(void))complete {
if (vapSources.count < 2) {
return;
}
RoomBoomExplosionView *explosionView = [[RoomBoomExplosionView alloc] initWithFrame:CGRectMake(0, 0, superView.bounds.size.width, superView.bounds.size.height)];
explosionView.userInteractionEnabled = NO;
explosionView.completeDisplay = complete;
[superView addSubview:explosionView];
explosionView.countDownURLString = [vapSources firstObject];
explosionView.endURLString = [vapSources lastObject];
[explosionView play_count];
}
- (void)saveCurrenRoomUID:(NSInteger)room {
}
- (void)dealloc
{
if (self.seq != 2) {
// 没有播放完就被移除,通知 manager
[[RoomBoomManager sharedManager] explosionEnd];
}
[self.vapView stopHWDMP4];
[self.vapView removeFromSuperview];
self.vapView = nil;
}
- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
self.seq = 0;
self.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.3];
[self addSubview:self.vapView];
[self.vapView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.mas_equalTo(self);
}];
}
return self;
}
//- (void)setModel:(Boom632Model *)model {
// _model = model;
// self.countDownURLString = model.countDownVapUrl;
// self.endURLString = model.endVapUrl;
// [self play_count];
//}
//
//- (void)setBoomInfo:(BoomInfoModel *)boomInfo {
// _boomInfo = boomInfo;
//}
- (void)play_count {
self.seq = 1;
NSString *path = [self.countDownURLString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (path.length == 0) {
return;
}
NSString *encodingUrl = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
@kWeakify(self);
[self.vapParser parseWithURL:encodingUrl completionBlock:^(NSString * _Nullable videoUrl) {
@kStrongify(self);
if (videoUrl.length) {
[self.vapView setMute:NO];
[self.vapView playHWDMP4:videoUrl repeatCount:0 delegate:self];
}
} failureBlock:^(NSError * _Nullable error) {
}];
}
- (void)play_boom {
self.seq = 2;
NSString *path = [self.endURLString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if (path.length == 0) {
return;
}
NSString *encodingUrl = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"`#%^{}\"[]|\\<> "].invertedSet];
@kWeakify(self);
[self.vapParser parseWithURL:encodingUrl completionBlock:^(NSString * _Nullable videoUrl) {
@kStrongify(self);
if (videoUrl.length) {
[self.vapView setMute:NO];
[self.vapView playHWDMP4:videoUrl repeatCount:0 delegate:self];
}
} failureBlock:^(NSError * _Nullable error) {
}];
}
#pragma mark - HWDMP4PlayDelegate
//即将开始播放时询问true马上开始播放false放弃播放
- (BOOL)shouldStartPlayMP4:(VAPView *)container config:(QGVAPConfigModel *)config {
return YES;
}
- (void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(VAPView *)container {
if (self.seq == 1) {
[self play_boom];
} else if (self.seq == 2) {
self.seq = 0;
@kWeakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@kStrongify(self);
[[RoomBoomManager sharedManager] explosionEnd];
[self removeFromSuperview];
});
}
}
- (void)viewDidStopPlayMP4:(NSInteger)lastFrameIndex view:(VAPView *)container {
}
- (void)viewDidFailPlayMP4:(NSError *)error{
@kWeakify(self);
dispatch_async(dispatch_get_main_queue(), ^{
@kStrongify(self);
[[RoomBoomManager sharedManager] explosionEnd];
[self removeFromSuperview];
});
}
#pragma mark -
- (VAPView *)vapView {
if (!_vapView) {
_vapView = [[VAPView alloc] init];
_vapView.contentMode = UIViewContentModeScaleAspectFill;
}
return _vapView;
}
- (XPRoomGiftAnimationParser *)vapParser {
if (!_vapParser) {
_vapParser = [[XPRoomGiftAnimationParser alloc] init];
}
return _vapParser;
}
@end