Files
yinmeng-ios/xplan-ios/Main/Mine/View/SubViews/MineInfo/XPMineUserInfoVoiceCardView.m

195 lines
6.9 KiB
Objective-C

//
// XPMineUserInfoVoiceCardView.m
// xplan-ios
//
// Created by 冯硕 on 2022/4/15.
//
#import "XPMineUserInfoVoiceCardView.h"
///Third
#import <Masonry/Masonry.h>
#import <AFNetworking.h>
///Tool
#import "UIImage+Utils.h"
#import "UIButton+EnlargeTouchArea.h"
#import "ThemeColor.h"
///Model
#import "MineSkillCardListInfoModel.h"
#import "XPSkillCardPlayerManager.h"
@interface XPMineUserInfoVoiceCardView ()
///背景
@property (nonatomic,strong) UIImageView *backImaegView;
///播放
@property (nonatomic,strong) UIButton *playButton;
///音符
@property (nonatomic,strong) UIImageView *noteImaegView;
///播放完成
@property (nonatomic,assign) BOOL isPlaying;
@end
@implementation XPMineUserInfoVoiceCardView
- (void)dealloc {
self.isPlaying = NO;
[self.noteImaegView stopAnimating];
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
[self initSubViews];
[self initSubViewConstraints];
}
return self;
}
#pragma mark - Private Method
- (void)initSubViews {
[self addSubview:self.backImaegView];
[self.backImaegView addSubview:self.playButton];
[self.backImaegView addSubview:self.noteImaegView];
}
- (void)initSubViewConstraints {
[self.backImaegView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.mas_equalTo(self);
make.right.mas_equalTo(self).offset(13);
}];
[self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(15, 15));
make.left.mas_equalTo(self.backImaegView).offset(6);
make.centerY.mas_equalTo(self.backImaegView);
}];
[self.noteImaegView mas_makeConstraints:^(MASConstraintMaker *make) {
make.size.mas_equalTo(CGSizeMake(60, 20));
make.centerY.mas_equalTo(self.backImaegView);
make.left.mas_equalTo(self.playButton.mas_right).offset(7);
}];
}
- (void)playButtonAction:(UIButton *)sender {
sender.selected = !sender.selected;
if (!self.isPlaying) {
if(self.voiceInfo.propVals.count) {
// NSString *fileName = [[self.voiceInfo.name componentsSeparatedByString:@"/"] lastObject];
NSString * url = [self.voiceInfo.propVals objectAtIndex:0];
NSArray * nameArray = [url componentsSeparatedByString:@"/"];
NSString * fileName = @"daeplay";
if (nameArray.count > 0) {
fileName = nameArray.lastObject;
}
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"mineSkillCardVoice"];
NSString *fullPath = [filePath stringByAppendingPathComponent:fileName];
NSLog(@"下载的路径:%@", fullPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
self.isPlaying = YES;
sender.selected = YES;
[self.noteImaegView startAnimating];
[[XPSkillCardPlayerManager shareInstance] playerVoiceWithPath:fullPath completionBlock:^{
self.isPlaying = NO;
[self.noteImaegView stopAnimating];
sender.selected = NO;
self.noteImaegView.image = [UIImage imageNamed:@"mine_voice_shengyin_32"];
[[XPSkillCardPlayerManager shareInstance] stopMusic];
}];
} else {
if (![[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
NSFileManager *fileMgr = [[NSFileManager alloc] init];
NSError * error;
[fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
NSLog(@"%@", error.description);
}
[self downloadAudioWithFileName:fileName musicUrl:url completion:^(BOOL isSuccess, NSString *editAudioPath) {
self.isPlaying = YES;
sender.selected = YES;
[self.noteImaegView startAnimating];
[[XPSkillCardPlayerManager shareInstance] playerVoiceWithPath:editAudioPath completionBlock:^{
self.isPlaying = NO;
sender.selected = NO;
[self.noteImaegView stopAnimating];
self.noteImaegView.image = [UIImage imageNamed:@"mine_voice_shengyin_32"];
[[XPSkillCardPlayerManager shareInstance] stopMusic];
}];
}];
}
}
} else {
self.isPlaying = NO;
sender.selected = NO;
[self.noteImaegView stopAnimating];
self.noteImaegView.image = [UIImage imageNamed:@"mine_voice_shengyin_32"];
[[XPSkillCardPlayerManager shareInstance] stopMusic];
}
}
- (void)downloadAudioWithFileName:(NSString *)fileName musicUrl:(NSString *)musicUrl completion:(void (^) (BOOL isSuccess, NSString *editAudioPath))completion {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:musicUrl]];
NSURLSessionDownloadTask *download = [manager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
NSString *filePath = [[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"mineSkillCardVoice"] stringByAppendingPathComponent:fileName];
return [NSURL fileURLWithPath:filePath];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
completion(YES, filePath.path);
} else {
completion(NO, nil);
}
}];
[download resume];
}
#pragma mark - Getters And Setters
- (UIImageView *)backImaegView {
if (!_backImaegView) {
_backImaegView = [[UIImageView alloc] init];
_backImaegView.userInteractionEnabled = YES;
_backImaegView.image = [UIImage gradientColorImageFromColors:@[UIColorFromRGB(0xF8F8F4), UIColorFromRGB(0xE7FBFD)] gradientType:GradientTypeLeftToRight imgSize:CGSizeMake(77, 25)];
_backImaegView.layer.masksToBounds = YES;
_backImaegView.layer.cornerRadius = 25/2;
}
return _backImaegView;
}
- (UIButton *)playButton {
if (!_playButton) {
_playButton = [UIButton buttonWithType:UIButtonTypeCustom];
[_playButton setImage:[UIImage imageNamed:@"mine_user_info_skill_card_voice_play"] forState:UIControlStateNormal];
[_playButton setImage:[UIImage imageNamed:@"mine_user_info_skill_card_voice_pause"] forState:UIControlStateSelected];
[_playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[_playButton setEnlargeEdgeWithTop:8 right:10 bottom:8 left:10];
}
return _playButton;
}
- (UIImageView *)noteImaegView {
if (!_noteImaegView) {
_noteImaegView = [[UIImageView alloc] init];
_noteImaegView.userInteractionEnabled = YES;
_noteImaegView.image = [UIImage imageNamed:@"mine_voice_shengyin_32"];
NSMutableArray * array = [NSMutableArray array];
for (int i = 0 ; i< 40; i++) {
UIImage * image;
if ( i < 10) {
image = [UIImage imageNamed:[NSString stringWithFormat:@"mine_voice_shengyin_0%d", i]];
} else {
image = [UIImage imageNamed:[NSString stringWithFormat:@"mine_voice_shengyin_%d", i]];
}
if (image) {
[array addObject:image];
}
}
_noteImaegView.animationImages = array;
_noteImaegView.animationDuration = 1.2;
_noteImaegView.animationRepeatCount = HUGE;
}
return _noteImaegView;
}
@end