467 lines
17 KiB
Objective-C
467 lines
17 KiB
Objective-C
//
|
||
// XPMineUserInfoRecordedSoundVC.m
|
||
// xplan-ios
|
||
//
|
||
// Created by duoban on 2022/12/29.
|
||
//
|
||
|
||
#import "XPMineUserInfoRecordedSoundVC.h"
|
||
#import "XPMineUserInfoRecordedSoundView.h"
|
||
#import <NIMSDK/NIMSDK.h>
|
||
#import <AVFoundation/AVFoundation.h>
|
||
#import "XPMineUserInfoPresenter.h"
|
||
#import "XPMineUserInfoProtocol.h"
|
||
#import <AFNetworking.h>
|
||
#import "XPSkillCardPlayerManager.h"
|
||
#import "CountDownHelper.h"
|
||
#import "TTNewAlertView.h"
|
||
#import "UploadFile.h"
|
||
|
||
UIKIT_EXTERN NSString * kUpdateSoundInfo;
|
||
|
||
@interface XPMineUserInfoRecordedSoundVC ()<CountDownHelperDelegate,NIMMediaManagerDelegate,XPMineUserInfoRecordedSoundViewDelegate>
|
||
@property (nonatomic,strong) XPMineUserInfoRecordedSoundView *recordSoundView;
|
||
///录音文件路径
|
||
@property (nonatomic,copy) NSString *filePath;
|
||
///当前录音时间
|
||
@property (nonatomic,assign) NSTimeInterval currentTime;
|
||
///总时间
|
||
@property (nonatomic,assign) NSTimeInterval totalTime;
|
||
///titleView
|
||
@property (nonatomic,strong) UILabel *titleView;
|
||
///返回按钮
|
||
@property (nonatomic,strong) UIButton *backBtn;
|
||
///是否正在播放录音
|
||
@property (nonatomic,assign) BOOL isPlaying;
|
||
///播放录音倒计时
|
||
@property (nonatomic,strong) CountDownHelper *countDownAdmin;
|
||
///是否没有保存录音文件
|
||
@property (nonatomic,assign) BOOL isNoSave;
|
||
|
||
|
||
|
||
@end
|
||
|
||
@implementation XPMineUserInfoRecordedSoundVC
|
||
|
||
- (XPMineUserInfoPresenter *)createPresenter {
|
||
return [[XPMineUserInfoPresenter alloc] init];
|
||
}
|
||
- (void)dealloc {
|
||
if([NIMSDK sharedSDK].mediaManager.isRecording){
|
||
[[NIMSDK sharedSDK].mediaManager stopRecord];
|
||
[[XPSkillCardPlayerManager shareInstance]recoverMicroAndVoiceWithBroadcast:NO];
|
||
}else{
|
||
[[XPSkillCardPlayerManager shareInstance] stopMusic];
|
||
if([XPSkillCardPlayerManager shareInstance].isMineInMic==YES && [XPSkillCardPlayerManager shareInstance].isPlay == YES){
|
||
[[XPSkillCardPlayerManager shareInstance]recoverMicroAndVoiceWithBroadcast:YES];
|
||
}
|
||
|
||
}
|
||
|
||
[self.countDownAdmin stopClockwise];
|
||
[[NIMSDK sharedSDK].mediaManager removeDelegate:self];
|
||
}
|
||
- (BOOL)isHiddenNavBar{
|
||
return YES;
|
||
}
|
||
-(void)backAction{
|
||
|
||
TTNewAlertView *alertView = [[TTNewAlertView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
|
||
@kWeakify(self)
|
||
alertView.confirmHandle = ^(){
|
||
@kStrongify(self)
|
||
[self.navigationController popViewControllerAnimated:YES];
|
||
};
|
||
BOOL isBack = NO;
|
||
NSString *message = @"";
|
||
if([NIMSDK sharedSDK].mediaManager.isRecording){
|
||
[self.recordSoundView recordAction];
|
||
return;
|
||
}
|
||
if(self.isNoSave == YES){
|
||
message = YMLocalizedString(@"XPMineUserInfoRecordedSoundVC8");
|
||
isBack = YES;
|
||
}
|
||
if(isBack == YES){
|
||
alertView.message = message;
|
||
[TTPopup popupView:alertView style:TTPopupStyleAlert];
|
||
return;
|
||
}
|
||
[self.navigationController popViewControllerAnimated:YES];
|
||
}
|
||
-(void)viewDidDisappear:(BOOL)animated{
|
||
[super viewDidDisappear:animated];
|
||
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
|
||
}
|
||
- (void)viewDidLoad {
|
||
[super viewDidLoad];
|
||
[self initSubViews];
|
||
[self initSubViewConstraints];
|
||
[self setUI];
|
||
|
||
|
||
|
||
}
|
||
#pragma mark - Private Method
|
||
- (void)initSubViews {
|
||
self.view.backgroundColor = [UIColor whiteColor];
|
||
[self.view addSubview:self.recordSoundView];
|
||
[self.view addSubview:self.titleView];
|
||
[self.view addSubview:self.backBtn];
|
||
[[NIMSDK sharedSDK].mediaManager addDelegate:self];
|
||
[NIMSDK sharedSDK].mediaManager.recordProgressUpdateTimeInterval = 0.01;
|
||
self.countDownAdmin.delegate = self;
|
||
self.countDownAdmin.seconds = 0.01;
|
||
self.isNoSave = NO;
|
||
self.totalTime = 60.0;
|
||
|
||
}
|
||
- (void)initSubViewConstraints {
|
||
[self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.top.mas_equalTo(kGetScaleWidth(54));
|
||
make.centerX.equalTo(self.view);
|
||
}];
|
||
[self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
|
||
make.width.height.mas_equalTo(kGetScaleWidth(28));
|
||
make.centerY.equalTo(self.titleView);
|
||
make.leading.mas_equalTo(kGetScaleWidth(20));
|
||
}];
|
||
}
|
||
#pragma mark -XPMineUserInfoProtocol
|
||
- (void)uploadVoiceFileToThirdSuccess:(NSString *)fileUrl{
|
||
NSInteger getCurrentTime = ceil(self.totalTime);
|
||
[self.presenter saveSoundWithUrl:fileUrl second:[NSString stringWithFormat:@"%ld",getCurrentTime]];
|
||
}
|
||
|
||
- (void)uploadVoiceFileFail:(NSString *)message{
|
||
[XNDJTDDLoadingTool hideOnlyView:self.view];
|
||
[self showErrorToast:message];
|
||
}
|
||
|
||
///保存声音成功
|
||
-(void)saveSoundSuccess:(NSString *)fileUrl{
|
||
[XNDJTDDLoadingTool hideOnlyView:self.view];
|
||
[self showSuccessToast:YMLocalizedString(@"XPMineUserInfoRecordedSoundVC4")];
|
||
[self.recordSoundView uploadRecordSuccess:NO];
|
||
self.soundModel = [XPSoundCardModel new];
|
||
self.soundModel.uid = self.uid;
|
||
self.soundModel.audioUrl = fileUrl;
|
||
NSInteger getCurrentTime = ceil(self.totalTime);
|
||
self.soundModel.second = getCurrentTime ?: 0;
|
||
self.soundModel.status = 1;
|
||
self.isNoSave = NO;
|
||
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
|
||
[[NSNotificationCenter defaultCenter]postNotificationName:kUpdateSoundInfo object:self.soundModel];
|
||
}
|
||
///保存声音失败
|
||
-(void)saveSoundFailWithMsg:(NSString *)msg{
|
||
[XNDJTDDLoadingTool hideOnlyView:self.view];
|
||
[self showErrorToast:msg];
|
||
}
|
||
-(void)deleteSoundSuccess{
|
||
[self showSuccessToast:YMLocalizedString(@"XPMineUserInfoRecordedSoundVC5")];
|
||
[self resetRecordAudio];
|
||
self.soundModel = nil;
|
||
[[NSNotificationCenter defaultCenter]postNotificationName:kUpdateSoundInfo object:nil];
|
||
}
|
||
-(void)setUI{
|
||
if(self.soundModel.status == 1 || self.soundModel.status == 2){
|
||
[self.recordSoundView uploadRecordSuccess:self.soundModel.status == 2];
|
||
self.filePath = self.soundModel.audioUrl;
|
||
self.totalTime = self.soundModel.second;
|
||
self.currentTime = 0;
|
||
NSInteger getCurrentTime = ceil(self.totalTime);
|
||
[self.recordSoundView recordingWithTime:[NSString stringWithFormat:@"0s/%lds",getCurrentTime] progress:0 animated:NO];
|
||
}
|
||
}
|
||
#pragma mark -CountDownHelperDelegate
|
||
///顺计时结束
|
||
- (void)onClockwiseFinish{
|
||
[self stopPlayAudioCompleted];
|
||
}
|
||
///顺计时进行
|
||
- (void)onClockwiseOpen:(CGFloat)time{
|
||
NSInteger getValue = floor(time);
|
||
NSInteger getCurrentTime = ceil(self.totalTime);
|
||
if(getValue > getCurrentTime){
|
||
getValue = getCurrentTime;
|
||
}
|
||
[self.recordSoundView recordingWithTime:[NSString stringWithFormat:@"%lds/%lds",getValue,getCurrentTime] progress:time / getCurrentTime animated:NO];
|
||
}
|
||
|
||
|
||
|
||
#pragma mark - XPMineUserInfoRecordedSoundViewDelegate
|
||
-(void)startRecordAudio{
|
||
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
|
||
if(authStatus != AVAuthorizationStatusAuthorized){
|
||
[self.recordSoundView stopRecord];
|
||
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted){
|
||
if(!granted){
|
||
dispatch_async(dispatch_get_main_queue(), ^{
|
||
TTAlertConfig *config = [[TTAlertConfig alloc]init];
|
||
config.message = YMLocalizedString(@"XPMineUserInfoRecordedSoundVC0");
|
||
[TTPopup alertWithConfig:config showBorder:NO confirmHandler:^{
|
||
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
|
||
if( [[UIApplication sharedApplication]canOpenURL:url] ) {
|
||
[[UIApplication sharedApplication]openURL:url options:@{}completionHandler:^(BOOL success) {
|
||
}];
|
||
}
|
||
|
||
} cancelHandler:^{
|
||
|
||
}];
|
||
});
|
||
|
||
}else{
|
||
dispatch_async(dispatch_get_main_queue(), ^{
|
||
[self startRecordAction];
|
||
});
|
||
|
||
}
|
||
}];
|
||
return;
|
||
}
|
||
[self startRecordAction];
|
||
}
|
||
-(void)startRecordAction{
|
||
if([XPSkillCardPlayerManager shareInstance].isMineInMic == YES){
|
||
[[XPSkillCardPlayerManager shareInstance]closeMicroAndVoiceWithBroadcast:NO];
|
||
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
|
||
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
|
||
[[NIMSDK sharedSDK].mediaManager recordForDuration:self.totalTime];
|
||
[self.recordSoundView startRecord];
|
||
});
|
||
return;
|
||
}
|
||
[[NIMSDK sharedSDK].mediaManager recordForDuration:self.totalTime];
|
||
[self.recordSoundView startRecord];
|
||
}
|
||
-(void)stopRecordAudio{
|
||
[[NIMSDK sharedSDK].mediaManager stopRecord];
|
||
}
|
||
-(void)resetRecordAudio{
|
||
self.isNoSave = NO;
|
||
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
|
||
[self.countDownAdmin stopClockwise];
|
||
[[XPSkillCardPlayerManager shareInstance] stopMusic];
|
||
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
|
||
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
|
||
self.filePath = nil;
|
||
self.currentTime = 0;
|
||
self.totalTime = 60.0;
|
||
[self.recordSoundView recordingWithTime:[NSString stringWithFormat:@"%.0fs/%.0fs",0.0,self.totalTime] progress:0 animated:NO];
|
||
[self.recordSoundView resetRecord];
|
||
});
|
||
|
||
|
||
}
|
||
-(void)playRecordAudio{
|
||
if(self.filePath.length==0)return;
|
||
[self.recordSoundView playRecord];
|
||
NSInteger getCurrentTime = ceil(self.totalTime);
|
||
[self.recordSoundView recordingWithTime:[NSString stringWithFormat:@"0s/%lds",getCurrentTime] progress:0 animated:NO];
|
||
if (!self.isPlaying) {
|
||
if(![self.filePath containsString:@"http"]){
|
||
[self playAudioWithUrl:self.filePath time:(int)getCurrentTime isDelay:[XPSkillCardPlayerManager shareInstance].isMineInMic];
|
||
return;
|
||
}
|
||
|
||
NSString *fileName = [[self.filePath componentsSeparatedByString:@"/"] lastObject];
|
||
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) safeObjectAtIndex1:0] stringByAppendingPathComponent:@"kMineSoundCard"];
|
||
NSString *fullPath = [filePath stringByAppendingPathComponent:fileName];
|
||
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]) {
|
||
[self playAudioWithUrl:fullPath time:(int)getCurrentTime isDelay:[XPSkillCardPlayerManager shareInstance].isMineInMic];
|
||
|
||
} else {
|
||
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
|
||
NSFileManager *fileMgr = [[NSFileManager alloc] init];
|
||
[fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
|
||
}
|
||
[UploadFile downloadAudioWithFileName:fileName musicUrl:self.filePath mainFileName:@"kMineSoundCard" completion:^(BOOL isSuccess, NSString *editAudioPath) {
|
||
if(isSuccess){
|
||
[self playAudioWithUrl:editAudioPath time:(int)getCurrentTime isDelay:[XPSkillCardPlayerManager shareInstance].isMineInMic];
|
||
|
||
}else{
|
||
[self stopPlayAudioCompleted];
|
||
}
|
||
|
||
|
||
}];
|
||
}
|
||
|
||
} else {
|
||
[self stopPlayAudioCompleted];
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
}
|
||
/// 播放录音
|
||
/// - Parameters:
|
||
/// - url: 录音链接
|
||
/// - time: 录音时间
|
||
/// - isDelay: 是否延时0.5秒播放录音,因为如果在麦上的话,需要先关麦再播放,所以要延时0.5秒来关麦后再播放,不然会没声音
|
||
-(void)playAudioWithUrl:(NSString*)url time:(int)time isDelay:(BOOL)isDelay{
|
||
if(isDelay == YES){
|
||
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
|
||
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
|
||
self.isPlaying = YES;
|
||
[self.countDownAdmin openClockwiseWithTime:time];
|
||
});
|
||
[[XPSkillCardPlayerManager shareInstance] playerNewVoiceWithPath:url completionBlock:^{
|
||
|
||
}];
|
||
return;
|
||
}
|
||
self.isPlaying = YES;
|
||
[self.countDownAdmin openClockwiseWithTime:time];
|
||
[[XPSkillCardPlayerManager shareInstance] playerVoiceWithPath:url completionBlock:^{
|
||
|
||
}];
|
||
}
|
||
-(void)stopPlayRecordAudio{
|
||
|
||
[[XPSkillCardPlayerManager shareInstance]recoverMicroAndVoiceWithBroadcast:[XPSkillCardPlayerManager shareInstance].isMineInMic];
|
||
[self stopPlayAudioCompleted];
|
||
|
||
}
|
||
- (void)deleteRecordingAudio{
|
||
|
||
TTNewAlertView *alertView = [[TTNewAlertView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
|
||
@kWeakify(self)
|
||
alertView.confirmHandle = ^(){
|
||
@kStrongify(self)
|
||
[self.presenter deleteSound];
|
||
};
|
||
alertView.message = YMLocalizedString(@"XPMineUserInfoRecordedSoundVC6");
|
||
|
||
[TTPopup popupView:alertView style:TTPopupStyleAlert];
|
||
}
|
||
-(void)saveRecordAudio{
|
||
|
||
if(self.filePath.length > 0){
|
||
[XNDJTDDLoadingTool showOnlyView:self.view];
|
||
[self.presenter uploadVoice:self.filePath];
|
||
}else{
|
||
[self showErrorToast:YMLocalizedString(@"XPMineUserInfoRecordedSoundVC3")];
|
||
}
|
||
|
||
}
|
||
///播放录音完成
|
||
-(void)stopPlayAudioCompleted{
|
||
[self.countDownAdmin stopClockwise];
|
||
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC));
|
||
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
|
||
[self.recordSoundView stopPlayRecord];
|
||
self.isPlaying = NO;
|
||
self.currentTime = 0;
|
||
NSInteger getCurrentTime = ceil(self.totalTime);
|
||
[self.recordSoundView recordingWithTime:[NSString stringWithFormat:@"0s/%lds",getCurrentTime] progress:0 animated:NO];
|
||
[[XPSkillCardPlayerManager shareInstance] stopMusic];
|
||
});
|
||
|
||
}
|
||
#pragma mark - NIMMediaManagerDelegate
|
||
/**
|
||
* 开始录制音频的回调
|
||
*
|
||
* @param filePath 录制的音频的文件路径
|
||
* @param error 错误信息
|
||
* @discussion 如果录音失败,filePath 有可能为 nil
|
||
*/
|
||
- (void)recordAudio:(nullable NSString *)filePath didBeganWithError:(nullable NSError *)error{
|
||
|
||
}
|
||
/**
|
||
* 录制音频完成后的回调
|
||
*
|
||
* @param filePath 录制完成的音频文件路径
|
||
* @param error 错误信息
|
||
*/
|
||
- (void)recordAudio:(nullable NSString *)filePath didCompletedWithError:(nullable NSError *)error{
|
||
if([XPSkillCardPlayerManager shareInstance].isMineInMic == YES){
|
||
[[XPSkillCardPlayerManager shareInstance]recoverMicroAndVoiceWithBroadcast:NO];
|
||
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC));
|
||
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
|
||
[self recordAudioCompletedActionWiftFilePath:filePath];
|
||
});
|
||
return;
|
||
}
|
||
[self recordAudioCompletedActionWiftFilePath:filePath];
|
||
}
|
||
|
||
-(void)recordAudioCompletedActionWiftFilePath:(nullable NSString *)filePath{
|
||
if(self.currentTime < 1){
|
||
self.filePath = nil;
|
||
self.currentTime = 0;
|
||
[self showErrorToast:YMLocalizedString(@"XPMineUserInfoRecordedSoundVC1")];
|
||
[self.recordSoundView recordingWithTime:[NSString stringWithFormat:@"%.0fs/%.0fs",0.0,self.totalTime] progress:0 animated:NO];
|
||
[self.recordSoundView stopRecord];
|
||
|
||
return;
|
||
}
|
||
self.isNoSave = YES;
|
||
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
|
||
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
|
||
}
|
||
self.filePath = filePath;
|
||
self.totalTime = self.currentTime;
|
||
self.currentTime = 0;
|
||
NSInteger getCurrentTime = ceil(self.totalTime);
|
||
[self.recordSoundView recordingWithTime:[NSString stringWithFormat:@"0s/%lds",getCurrentTime] progress:0 animated:NO];
|
||
[self.recordSoundView recordCompleted];
|
||
|
||
}
|
||
|
||
|
||
/**
|
||
* 音频录制进度更新回调
|
||
*
|
||
* @param currentTime 当前录制的时间
|
||
*/
|
||
- (void)recordAudioProgress:(NSTimeInterval)currentTime{
|
||
self.currentTime = currentTime;
|
||
if(self.currentTime > 60){
|
||
self.currentTime = 60;
|
||
}
|
||
NSInteger getCurrentTime = ceil(self.currentTime);
|
||
[self.recordSoundView recordingWithTime:[NSString stringWithFormat:@"%lds/%.0fs",getCurrentTime,self.totalTime] progress:currentTime / self.totalTime animated:NO];
|
||
}
|
||
|
||
|
||
#pragma mark -懒加载
|
||
- (XPMineUserInfoRecordedSoundView *)recordSoundView{
|
||
if (!_recordSoundView){
|
||
_recordSoundView = [[XPMineUserInfoRecordedSoundView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
|
||
_recordSoundView.delegate = self;
|
||
}
|
||
return _recordSoundView;
|
||
}
|
||
-(UILabel *)titleView{
|
||
if (!_titleView){
|
||
_titleView = [UILabel labelInitWithText:YMLocalizedString(@"XPMineUserInfoRecordedSoundVC2") font:kFontSemibold(17) textColor:[DJDKMIMOMColor inputTextColor]];
|
||
|
||
}
|
||
return _titleView;
|
||
}
|
||
-(UIButton *)backBtn{
|
||
if (!_backBtn){
|
||
_backBtn = [UIButton new];
|
||
[_backBtn setImage:[kImage(@"common_nav_back")ms_SetImageForRTL] forState:UIControlStateNormal];
|
||
[_backBtn setEnlargeEdgeWithTop:10 right:10 bottom:10 left:10];
|
||
[_backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
|
||
}
|
||
return _backBtn;
|
||
}
|
||
-(CountDownHelper *)countDownAdmin{
|
||
if (!_countDownAdmin){
|
||
_countDownAdmin = [CountDownHelper new];
|
||
}
|
||
return _countDownAdmin;
|
||
}
|
||
@end
|