Files
featherVoice/QXLive/Room(房间)/View/礼物特效/QXCPEffectView.m

292 lines
10 KiB
Mathematica
Raw Normal View History

2025-11-28 22:43:06 +08:00
//
// QXCPEffectView.m
// QXLive
//
// Created by on 2025/11/22.
//
#import "QXCPEffectView.h"
#import "QXEffectSvgaView.h"
#import "QXFileManager.h"
@interface QXCPEffectView()<HWDMP4PlayDelegate>
@property (nonatomic,strong)UIView *bgView;
@property (nonatomic,strong)QXSeatHeaderView *leftHeadeView;
@property (nonatomic,strong)QXSeatHeaderView *rightHeadeView;
@property (nonatomic,strong)QXEffectSvgaView *svagView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic, strong) NSRecursiveLock *lock;
@property (nonatomic, assign) BOOL isLoadEffect;
@property (nonatomic, strong) QXRoomChatListModel *playModel;
@property (nonatomic, assign) BOOL isPlaying;
2025-12-04 14:11:00 +08:00
@property (nonatomic, assign) BOOL isBackground;
2025-11-28 22:43:06 +08:00
@end
@implementation QXCPEffectView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = NO;
self.isLoadEffect = NO;
[self initSubviews];
}
return self;
}
-(void)initSubviews{
self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, ScaleWidth(600))];
[self addSubview:self.bgView];
// [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
// make.left.right.equalTo(self);
// make.height.equalTo(self.mas_width);
// make.top.mas_equalTo(SCREEN_HEIGHT);
// }];
[self.bgView addSubview:self.svagView];
[self.bgView addSubview:self.playerMp4View];
[self.svagView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.bgView);
}];
[self.playerMp4View mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self.bgView);
}];
self.leftHeadeView = [[QXSeatHeaderView alloc] initWithFrame:CGRectMake((self.bgView.width-60)/2-3, (self.bgView.height-60)/2-3, 60, 60)];
self.leftHeadeView.hidden = YES;
[self.leftHeadeView addRoundedCornersWithRadius:30];
[self.bgView addSubview:self.leftHeadeView];
[self.leftHeadeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerX.equalTo(self.bgView).offset(-32);
make.centerY.equalTo(self.bgView).offset(80);
make.width.height.mas_equalTo(60);
}];
self.rightHeadeView = [[QXSeatHeaderView alloc] initWithFrame:CGRectMake(self.leftHeadeView.right+3, self.leftHeadeView.top, 60, 60)];
self.rightHeadeView.hidden = YES;
[self.bgView addSubview:self.rightHeadeView];
[self.rightHeadeView addRoundedCornersWithRadius:30];
[self.rightHeadeView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.leftHeadeView.mas_right).offset(2);
make.centerY.equalTo(self.leftHeadeView);
make.width.height.mas_equalTo(60);
}];
}
-(void)displayCpEffectUrl:(QXRoomChatListModel *)model{
2025-12-04 14:11:00 +08:00
if (self.isBackground) {
return;
}
2025-11-28 22:43:06 +08:00
NSString *play_image = model.rights_icon;
dispatch_async(self.queue, ^{
/// play_imagereturn
if (![play_image isExist]) {
return;
}
/// play_image
NSString *pathExtension = [play_image pathExtension].lowercaseString;
/// svgamp4 return
if (!([pathExtension isEqualToString:@"svga"] || [pathExtension isEqualToString:@"mp4"])) {
return;
}
/// list
[self.lock lock];
/// list
[self.dataArray addObject:model];
NSLog(@"当前礼物有---%ld个",self.dataArray.count);
///
[self.lock unlock];
///
if (self.isLoadEffect == NO) {
///
self.isLoadEffect = YES;
[self loadStartSVGAPlayer];
}
});
}
-(void)stopPlay{
2025-12-04 14:11:00 +08:00
self.isBackground = YES;
2025-11-28 22:43:06 +08:00
// [self removeSvgaQueueData];
self.isLoadEffect = NO;
[self.svagView stopEffectSvgaPlay];
// [self.playerView stop];
[self.playerMp4View stopHWDMP4];
// [self.alphaVideoView stop];
self.playerMp4View.hidden = YES;
self.svagView.hidden = YES;
2025-12-04 14:11:00 +08:00
[self hideEffect];
2025-11-28 22:43:06 +08:00
}
-(void)startPlay{
2025-12-04 14:11:00 +08:00
self.isBackground = NO;
2025-11-28 22:43:06 +08:00
[self loadStartSVGAPlayer];
}
-(void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(VAPView *)container{
dispatch_async(dispatch_get_main_queue(), ^{
self.playerMp4View.hidden = YES;
});
dispatch_async(dispatch_get_main_queue(), ^{
[self hideEffect];
[self loadStartSVGAPlayer];
});
}
-(void)viewDidFailPlayMP4:(NSError *)error{
dispatch_async(dispatch_get_main_queue(), ^{
self.playerMp4View.hidden = YES;
});
dispatch_async(dispatch_get_main_queue(), ^{
[self hideEffect];
[self loadStartSVGAPlayer];
});
}
- (void)loadStartSVGAPlayer {
NSString *play_image = nil;
/// list
[self.lock lock];
/// list0
if (self.dataArray.count > 0) {
/// giftlist
QXRoomChatListModel *model = self.dataArray.firstObject;
play_image = model.rights_icon;
self.playModel = model;
/// list
[self.dataArray removeObjectAtIndex:0];
self.isLoadEffect = YES;
}else {
self.isLoadEffect = NO;
}
///
[self.lock unlock];
if (self.isLoadEffect && [play_image isExist]) {
dispatch_async(dispatch_get_main_queue(), ^{
if ([play_image hasSuffix:@"mp4"]) {
__weak typeof(self)weakSelf = self;
/// mp4
[[QXRequset shareInstance] downloadVideoPlayerWithUrl:play_image completion:^(BOOL result, NSString * _Nonnull fileName) {
if (result) {
NSString *videoPath = [QXFileManager getGiftVideoPath:fileName];
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.playerMp4View.hidden = NO;
weakSelf.svagView.hidden = YES;
// [weakSelf showEffect];
[weakSelf performSelector:@selector(showEffect) afterDelay:0.5];
});
[weakSelf.playerMp4View stopHWDMP4];
[weakSelf.playerMp4View playHWDMP4:videoPath delegate:self];
}else{
[weakSelf loadStartSVGAPlayer];
}
}];
}else if ([play_image hasSuffix:@"svg"] || [play_image hasSuffix:@"svga"]) {
__weak typeof(self)weakSelf = self;
/// svga
[[QXRequset shareInstance] downloadVideoPlayerWithUrl:play_image completion:^(BOOL result, NSString * _Nonnull fileName) {
if (result) {
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.playerMp4View.hidden = YES;
weakSelf.svagView.hidden = NO;
});
NSString *filePath = [QXFileManager getGiftVideoPath:fileName];
[weakSelf.svagView loadSVGAPlayerWith:filePath];
}else{
[weakSelf loadStartSVGAPlayer];
}
}];
}else {
[self.lock lock];
self.isLoadEffect = NO;
[self.lock unlock];
[self loadStartSVGAPlayer];
}
});
}
}
- (void)loadEndSvgaPlayer {
dispatch_async(self.queue, ^{
[self hideEffect];
[self loadStartSVGAPlayer];
});
}
- (void)destroyEffectView {
[self removeSvgaQueueData];
[self.svagView destroySvga];
// [self.playerView destroyPlayer];
[self.playerMp4View stopHWDMP4];
[self.svagView removeFromSuperview];
[self.playerMp4View removeFromSuperview];
[self.leftHeadeView removeFromSuperview];
[self.rightHeadeView removeFromSuperview];
_svagView = nil;
// _playerView = nil;
_playModel = nil;
_playerMp4View = nil;
// _alphaVideoView = nil;
}
- (void)removeSvgaQueueData {
[self.lock lock];
[self.dataArray removeAllObjects];
self.isLoadEffect = NO;
[self.lock unlock];
}
-(void)showEffect{
self.leftHeadeView.hidden = NO;
self.rightHeadeView.hidden = NO;
[self.leftHeadeView setHeadIcon:self.playModel.FromUserInfo.avatar dress:self.playModel.FromUserInfo.dress];
[self.rightHeadeView setHeadIcon:self.playModel.ToUserInfo.avatar dress:self.playModel.ToUserInfo.avatar];
// [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
// make.top.mas_equalTo(SCREEN_HEIGHT);
// }];
[self.leftHeadeView startSimpleFloat];
[self.rightHeadeView startSimpleFloat];
self.bgView.top = SCREEN_HEIGHT;
self.bgView.alpha = 1;
[UIView animateWithDuration:0.3 animations:^{
self.bgView.top = 0;
}];
}
-(void)hideEffect{
self.bgView.alpha = 0;
// [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
// make.top.mas_equalTo(SCREEN_HEIGHT);
// }];
self.bgView.top = SCREEN_HEIGHT;
self.leftHeadeView.hidden = YES;
self.rightHeadeView.hidden = YES;
[self.leftHeadeView stopSimpleFloat];
[self.rightHeadeView stopSimpleFloat];
}
- (QXEffectSvgaView *)svagView {
if (!_svagView) {
_svagView = [[QXEffectSvgaView alloc] initWithFrame:CGRectZero isAutoPlay:YES];
// __weak typeof(self)weakSelf = self;
_svagView.didFinishedDisplay = ^(QXEffectSvgaView * _Nonnull svgaView) {
[svgaView destroySvga];
};
}
return _svagView;
}
-(VAPView *)playerMp4View{
if (!_playerMp4View) {
_playerMp4View = [[VAPView alloc] initWithFrame:CGRectZero];
_playerMp4View.hwd_enterBackgroundOP = HWDMP4EBOperationTypeStop;
_playerMp4View.contentMode = UIViewContentModeScaleAspectFit;
_playerMp4View.userInteractionEnabled = NO;
_playerMp4View.backgroundColor = [UIColor clearColor];
// _playerMp4View.hwd_Delegate = self;
}
return _playerMp4View;
}
-(NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray array];
}
return _dataArray;
}
@end