Files
midi_ios/QXLive/HomePage(声播)/View/礼物/QXGiftPlayerManager.m

394 lines
13 KiB
Mathematica
Raw Normal View History

2025-08-14 10:07:49 +08:00
//
// QXGiftPlayer.m
// QXLive
//
// Created by on 2025/5/8.
//
#import "QXGiftPlayerManager.h"
#import "QXEffectSvgaView.h"
#import "QXFileManager.h"
2025-09-15 18:20:22 +08:00
#import "FMDB.h"
static NSString *TABLENAME = @"qxGitfPlayTable";
static NSString *ID = @"id";
static NSString *PLAY_IMAGE = @"play_image";
2025-08-14 10:07:49 +08:00
@interface QXGiftPlayerManager()
2025-09-15 18:20:22 +08:00
@property (nonatomic,strong) FMDatabase *fmdb;
2025-08-14 10:07:49 +08:00
@property (nonatomic,strong) UIView *bgEffectView;
//
@property (nonatomic,strong) QXEffectContentView *fullEffectView;
//
@property (nonatomic,strong) QXEffectContentView *chatEffectView;
@end
@implementation QXGiftPlayerManager
+ (instancetype)shareManager{
static QXGiftPlayerManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
manager = [[QXGiftPlayerManager alloc] init];
2025-09-15 18:20:22 +08:00
// [manager creatDatabase];
2025-08-14 10:07:49 +08:00
});
return manager;
}
2025-09-15 18:20:22 +08:00
//
//- (NSString *)databaseFilePath
//{
// NSArray *filePath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// NSString *documentPath = [filePath objectAtIndex:0];
// NSString *dbFilePath = [documentPath stringByAppendingPathComponent:@"qx_gift.db"];
// return dbFilePath;
//}
//
////
//- (void)creatDatabase
//{
// _fmdb = [FMDatabase databaseWithPath:[self databaseFilePath]];
// //,
// [_fmdb setShouldCacheStatements:YES];
// [_fmdb open];
// NSString *sqlCreateTable = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' INTEGER PRIMARY KEY AUTOINCREMENT, '%@' TEXT)",TABLENAME,ID,PLAY_IMAGE];
// BOOL res = [_fmdb executeUpdate:sqlCreateTable];
// NSLog(@"%@",res?@"数据库创建成功":@"数据库创建失败");
// [_fmdb close];
//}
//
//-(void)insertGiftWithPlayImage:(NSString*)giftImage{
// if ([_fmdb open]) {
// NSString *insertSql1= [NSString stringWithFormat:
// @"INSERT INTO '%@' ('%@') VALUES ('%@');",
// TABLENAME, PLAY_IMAGE, giftImage];
// BOOL res = [_fmdb executeUpdate:insertSql1];
// NSLog(@"%@",res?@"礼物添加成功":@"礼物添加失败");
// [_fmdb close];
// }
//}
//- (BOOL)deleteRecordWithId:(NSInteger)recordId {
// if ([_fmdb open]) {
// NSString *deleteSQL = [NSString stringWithFormat:
// @"DELETE FROM %@ WHERE id = ('%ld');",
// TABLENAME, recordId];
// BOOL success = [_fmdb executeUpdate:deleteSQL];
// if (!success) {
// NSLog(@"删除失败: %@", [_fmdb lastErrorMessage]);
// }else{
// NSLog(@"删除成功");
// }
// [_fmdb close];
// return success;
// }
// return NO;
//}
//
//-(BOOL)deleFirstImage{
// if ([_fmdb open]) {
// NSString *querySQL= [NSString stringWithFormat:
// @"SELECT id FROM '%@' ORDER BY id ASC LIMIT 1",
// TABLENAME];
// FMResultSet *resultSet = [_fmdb executeQuery:querySQL];
// [_fmdb close];
// if ([resultSet next]) {
// NSInteger recordId = [resultSet intForColumn:@"id"];
// return [self deleteRecordWithId:recordId];
// }
// }
// return NO;
//}
//
//-(NSString*)readFirstImage{
// if ([_fmdb open]) {
// NSString *querySQL= [NSString stringWithFormat:
// @"SELECT id FROM '%@' ORDER BY id ASC LIMIT 1",
// TABLENAME];
// FMResultSet *resultSet = [_fmdb executeQuery:querySQL];
// [_fmdb close];
// if ([resultSet next]) {
// NSString *playImage = [resultSet stringForColumn:PLAY_IMAGE];
// return playImage;
// }
// }
// return nil;
//}
2025-08-14 10:07:49 +08:00
- (UIView *)defaultBgEffectView {
return self.bgEffectView;
}
///
- (QXEffectContentView *)defaultFullEffectView {
return self.fullEffectView;
}
///
- (QXEffectContentView *)defaultChatEffectView {
return self.chatEffectView;
}
2025-09-15 18:20:22 +08:00
- (void)displayFullEffectView:(NSString *)gift {
// [self insertGiftWithPlayImage:gift];
2025-08-14 10:07:49 +08:00
[self.fullEffectView displayEffectView:gift];
}
2025-09-15 18:20:22 +08:00
- (void)displayChatEffectView:(NSString *)gift {
2025-08-14 10:07:49 +08:00
[self.chatEffectView displayEffectView:gift];
}
///
- (void)openOrCloseEffectViewWith:(BOOL)isShow {
[self.fullEffectView openOrCloseEffectViewWith:isShow];
[self.chatEffectView openOrCloseEffectViewWith:isShow];
}
///
- (void)destroyEffectSvga {
[self.fullEffectView destroyEffectView];
[self.chatEffectView destroyEffectView];
[self.fullEffectView removeFromSuperview];
[self.chatEffectView removeFromSuperview];
[self.bgEffectView removeFromSuperview];
_fullEffectView = nil;
_chatEffectView = nil;
_bgEffectView = nil;
}
-(void)stopPlay{
[self.fullEffectView stopPlay];
[self.chatEffectView stopPlay];
}
- (UIView *)bgEffectView {
if (!_bgEffectView) {
_bgEffectView = [[UIView alloc] init];
_bgEffectView.userInteractionEnabled = NO;
[_bgEffectView addSubview:self.fullEffectView];
[_bgEffectView addSubview:self.chatEffectView];
}
return _bgEffectView;
}
- (QXEffectContentView *)fullEffectView {
if (!_fullEffectView) {
_fullEffectView = [[QXEffectContentView alloc] initWithFrame:CGRectZero];
_fullEffectView.queue = dispatch_queue_create("qx_room_full_svga_message.com", NULL);
}
return _fullEffectView;
}
- (QXEffectContentView *)chatEffectView {
if (!_chatEffectView) {
_chatEffectView = [[QXEffectContentView alloc] initWithFrame:CGRectZero];
[_chatEffectView.playerMp4View mas_remakeConstraints:^(MASConstraintMaker *make) {
// make.centerX.centerY.equalTo(_chatEffectView);
// make.width.mas_equalTo(ScaleWidth(375));
// make.height.mas_equalTo(ScaleWidth(375));
make.edges.equalTo(_chatEffectView);
}];
_chatEffectView.queue = dispatch_queue_create("qx_room_chat_svga_message.com", NULL);
}
return _chatEffectView;
}
@end
@interface QXEffectContentView ()<HWDMP4PlayDelegate>
@property (nonatomic,strong) QXEffectSvgaView *svagView;
@property (nonatomic,strong) QXGiftModel *playModel;
@property (nonatomic, assign) BOOL isLoadEffect; ///<
@property (nonatomic, strong) NSRecursiveLock *lock;
@property (nonatomic, strong) NSMutableArray *giftArray;
@end
@implementation QXEffectContentView
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.userInteractionEnabled = NO;
self.isLoadEffect = NO;
2025-09-15 18:20:22 +08:00
[self addSubview:self.svagView];
2025-08-14 10:07:49 +08:00
[self addSubview:self.playerMp4View];
2025-09-15 18:20:22 +08:00
[self.svagView mas_makeConstraints:^(MASConstraintMaker *make) {
2025-08-14 10:07:49 +08:00
make.edges.equalTo(self);
}];
[self.playerMp4View mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
}
return self;
}
2025-09-15 18:20:22 +08:00
/// gift
- (void)displayEffectView:(NSString *)play_image {
2025-08-14 10:07:49 +08:00
dispatch_async(self.queue, ^{
2025-09-15 18:20:22 +08:00
/// play_imagereturn
if (![play_image isExist]) {
2025-08-14 10:07:49 +08:00
return;
}
2025-09-15 18:20:22 +08:00
/// play_image
NSString *pathExtension = [play_image pathExtension].lowercaseString;
/// svgamp4 return
2025-08-14 10:07:49 +08:00
if (!([pathExtension isEqualToString:@"svga"] || [pathExtension isEqualToString:@"mp4"])) {
return;
}
2025-09-15 18:20:22 +08:00
/// list
2025-08-14 10:07:49 +08:00
[self.lock lock];
2025-09-15 18:20:22 +08:00
/// list
[self.giftArray addObject:play_image];
NSLog(@"当前礼物有---%ld个",self.giftArray.count);
///
2025-08-14 10:07:49 +08:00
[self.lock unlock];
2025-09-15 18:20:22 +08:00
///
2025-08-14 10:07:49 +08:00
if (self.isLoadEffect == NO) {
2025-09-15 18:20:22 +08:00
///
2025-08-14 10:07:49 +08:00
self.isLoadEffect = YES;
[self loadStartSVGAPlayer];
}
});
}
- (void)openOrCloseEffectViewWith:(BOOL)isShow {
_isShow = isShow;
[self removeSvgaQueueData];
2025-09-15 18:20:22 +08:00
[self.svagView stopEffectSvgaPlay];
2025-08-14 10:07:49 +08:00
// [self.playerView stop];
[self.playerMp4View stopHWDMP4];
// [self.alphaVideoView stop];
self.playerMp4View.hidden = YES;
[self setHidden:!isShow];
}
-(void)stopPlay{
[self removeSvgaQueueData];
2025-09-15 18:20:22 +08:00
[self.svagView stopEffectSvgaPlay];
2025-08-14 10:07:49 +08:00
// [self.playerView stop];
[self.playerMp4View stopHWDMP4];
// [self.alphaVideoView stop];
self.playerMp4View.hidden = YES;
}
-(void)viewDidFinishPlayMP4:(NSInteger)totalFrameCount view:(VAPView *)container{
dispatch_async(dispatch_get_main_queue(), ^{
self.playerMp4View.hidden = YES;
});
2025-09-15 18:20:22 +08:00
dispatch_async(dispatch_get_main_queue(), ^{
[self loadStartSVGAPlayer];
});
2025-08-14 10:07:49 +08:00
}
- (void)loadStartSVGAPlayer {
if (!_isShow) {
2025-09-15 18:20:22 +08:00
/// isshow return
2025-08-14 10:07:49 +08:00
return;
}
2025-09-15 18:20:22 +08:00
NSString *play_image = nil;
/// list
2025-08-14 10:07:49 +08:00
[self.lock lock];
2025-09-15 18:20:22 +08:00
/// list0
2025-08-14 10:07:49 +08:00
if (self.giftArray.count > 0) {
2025-09-15 18:20:22 +08:00
/// giftlist
play_image = self.giftArray.firstObject;
/// list
2025-08-14 10:07:49 +08:00
[self.giftArray removeObjectAtIndex:0];
self.isLoadEffect = YES;
}else {
self.isLoadEffect = NO;
}
2025-09-15 18:20:22 +08:00
///
2025-08-14 10:07:49 +08:00
[self.lock unlock];
2025-09-15 18:20:22 +08:00
if (self.isLoadEffect && [play_image isExist]) {
2025-08-14 10:07:49 +08:00
dispatch_async(dispatch_get_main_queue(), ^{
2025-09-15 18:20:22 +08:00
if ([play_image hasSuffix:@"mp4"]) {
2025-08-14 10:07:49 +08:00
__weak typeof(self)weakSelf = self;
2025-09-15 18:20:22 +08:00
/// mp4
[[QXRequset shareInstance] downloadVideoPlayerWithUrl:play_image completion:^(BOOL result, NSString * _Nonnull fileName) {
2025-08-14 10:07:49 +08:00
NSString *videoPath = [QXFileManager getGiftVideoPath:fileName];
2025-09-15 18:20:22 +08:00
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.playerMp4View.hidden = NO;
weakSelf.svagView.hidden = YES;
});
2025-08-14 10:07:49 +08:00
[weakSelf.playerMp4View playHWDMP4:videoPath delegate:self];
}];
2025-09-15 18:20:22 +08:00
}else if ([play_image hasSuffix:@"svg"] || [play_image hasSuffix:@"svga"]) {
2025-08-14 10:07:49 +08:00
__weak typeof(self)weakSelf = self;
2025-09-15 18:20:22 +08:00
/// svga
[[QXRequset shareInstance] downloadVideoPlayerWithUrl:play_image completion:^(BOOL result, NSString * _Nonnull fileName) {
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.playerMp4View.hidden = YES;
weakSelf.svagView.hidden = NO;
});
2025-08-14 10:07:49 +08:00
NSString *filePath = [QXFileManager getGiftVideoPath:fileName];
2025-09-15 18:20:22 +08:00
[weakSelf.svagView loadSVGAPlayerWith:filePath];
2025-08-14 10:07:49 +08:00
}];
}else {
[self.lock lock];
self.isLoadEffect = NO;
[self.lock unlock];
}
});
}
}
- (void)loadEndSvgaPlayer {
dispatch_async(self.queue, ^{
[self loadStartSVGAPlayer];
});
}
- (void)destroyEffectView {
[self removeSvgaQueueData];
[self.svagView destroySvga];
// [self.playerView destroyPlayer];
[self.playerMp4View stopHWDMP4];
2025-09-15 18:20:22 +08:00
[self.svagView removeFromSuperview];
2025-08-14 10:07:49 +08:00
[self.playerMp4View removeFromSuperview];
_svagView = nil;
// _playerView = nil;
_playModel = nil;
_playerMp4View = nil;
// _alphaVideoView = nil;
}
- (void)removeSvgaQueueData {
[self.lock lock];
[self.giftArray removeAllObjects];
self.isLoadEffect = NO;
[self.lock unlock];
}
-(NSMutableArray *)giftArray{
if (!_giftArray) {
_giftArray = [NSMutableArray array];
}
return _giftArray;
}
2025-09-15 18:20:22 +08:00
- (QXEffectSvgaView *)svagView {
2025-08-14 10:07:49 +08:00
if (!_svagView) {
_svagView = [[QXEffectSvgaView alloc] initWithFrame:CGRectZero isAutoPlay:YES];
__weak typeof(self)weakSelf = self;
_svagView.didFinishedDisplay = ^(QXEffectSvgaView * _Nonnull svgaView) {
[svgaView destroySvga];
[weakSelf loadEndSvgaPlayer];
};
}
return _svagView;
}
-(VAPView *)playerMp4View{
if (!_playerMp4View) {
_playerMp4View = [[VAPView alloc] initWithFrame:CGRectZero];
_playerMp4View.hwd_enterBackgroundOP = HWDMP4EBOperationTypePauseAndResume;
_playerMp4View.contentMode = UIViewContentModeScaleAspectFit;
_playerMp4View.userInteractionEnabled = NO;
_playerMp4View.backgroundColor = [UIColor clearColor];
// _playerMp4View.hwd_Delegate = self;
}
return _playerMp4View;
}
@end