292 lines
10 KiB
Objective-C
292 lines
10 KiB
Objective-C
//
|
||
// 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;
|
||
@property (nonatomic, assign) BOOL isBackground;
|
||
@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{
|
||
if (self.isBackground) {
|
||
return;
|
||
}
|
||
NSString *play_image = model.rights_icon;
|
||
dispatch_async(self.queue, ^{
|
||
/// 如果play_image不存在return
|
||
if (![play_image isExist]) {
|
||
return;
|
||
}
|
||
/// 将play_image链接转为小写
|
||
NSString *pathExtension = [play_image pathExtension].lowercaseString;
|
||
/// 判定礼物的后缀是否为svga或者mp4如果非这两种 则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{
|
||
self.isBackground = YES;
|
||
// [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;
|
||
[self hideEffect];
|
||
}
|
||
|
||
-(void)startPlay{
|
||
self.isBackground = NO;
|
||
[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];
|
||
/// 如果list长度大于0
|
||
if (self.dataArray.count > 0) {
|
||
/// gift的实体类则赋值,取list中的第一个数据
|
||
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
|