123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721 |
- //
- // YMRecordSoundView.m
- // MSYOUPAI
- //
- // Created by YoMi on 2024/2/25.
- // Copyright © 2024 MS. All rights reserved.
- //
- #import "YMRecordSoundView.h"
- #import "NSURL+TransferFileFormat.h"
- #import "YMCircleView.h"
- @interface YMRecordSoundView ()<AVAudioPlayerDelegate,AVAudioRecorderDelegate>
- /// 是否需要提交按钮
- @property (nonatomic, assign) BOOL isNeedSubmitButton;
- /// 操作按钮尺寸
- @property (nonatomic, assign) CGSize operationSize;
- /// 录音状态
- @property (nonatomic, assign) YMRecordSoundStatusType recordSoundStatusType;
- /// 录制时长标签
- @property (nonatomic, strong) UILabel *recordDurationLb;
- /// 圆形视图
- @property (nonatomic, strong) YMCircleView *circleView;
- /// 录制按钮
- @property (nonatomic, strong) UIButton *recordBtn;
- /// 描述标签
- @property (nonatomic, strong) UILabel *descLb;
- /// 重录按钮
- @property (nonatomic, strong) UIButton *rerecordBtn;
- /// 提交按钮
- @property (nonatomic, strong) UIButton *submitBtn;
- /// 音频播放器
- @property (nonatomic, strong) AVAudioPlayer *audioPlayer;
- /// 音频录制器
- @property (nonatomic, strong) AVAudioRecorder *audioRecorder;
- /// 录制音频链接
- @property (nonatomic, strong) NSURL *recordAudioUrl;
- /// 录音时间
- @property (nonatomic, assign) NSInteger audioTime;
- /// 播放时间
- @property (nonatomic, assign) NSInteger playTime;
- @property (nonatomic, strong) NSTimer *recordTimer;
- @end
- @implementation YMRecordSoundView
- - (instancetype)initWithIsNeedSubmitButton:(BOOL)isNeedSubmitButton OperationSize:(CGSize)operationSize{
- if (self = [super init]) {
- self.isNeedSubmitButton = isNeedSubmitButton;
- self.operationSize = operationSize;
-
- [self setupViews];
- }
- return self;
- }
- - (void)setupViews{
-
- self.minDuration = 2;
- self.maxDuration = 8;
-
- self.rerecordBgColor = HexColorFromRGB(0xF5F5F5);
- self.submitBgColor = HexColorFromRGB(0xF888E7);
-
- self.rerecordTitleColor = HexColorFromRGB(0x555555);
- self.submitTitleColor = HexColorFromRGB(0xFFFFFF);
-
- self.rerecordFont = LCFont(14);
- self.submitFont = LCFont(14);
-
- self.rerecordTitle = @"重录";
- self.submitTitle = @"确定";
-
- self.rerecordRadius = adapt(10);
- self.submitRadius = adapt(10);
-
- self.rerecordBorderColor = HexColorFromRGB(0xF5F5F5);
- self.submitBorderColor = HexColorFromRGB(0xF888E7);
-
- self.rerecordBorderWidth = adapt(0.5);
- self.submitBorderWidth = adapt(0.5);
-
- self.exportType = YMRecordSoundExportTypePM3;
- self.recordSoundStatusType = YMRecordSoundStatusTypeNone;
-
- [self addSubview:self.recordDurationLb];
- [self addSubview:self.circleView];
- [self addSubview:self.recordBtn];
- [self addSubview:self.descLb];
- [self addSubview:self.rerecordBtn];
- [self addSubview:self.submitBtn];
- [self setNeedsUpdateConstraints];
- [self updateConstraintsIfNeeded];
- }
- - (void)updateConstraints{
- [self.recordDurationLb mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.mas_centerX);
- make.top.equalTo(self).offset(adapt(10));
- }];
-
- [self.circleView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.mas_centerX);
- make.top.equalTo(self.recordDurationLb.mas_bottom).offset(adapt(15));
- make.width.height.mas_equalTo(adapt(75));
- }];
-
- [self.recordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.circleView.mas_centerX);
- make.centerY.equalTo(self.circleView.mas_centerY);
- make.width.height.mas_equalTo(adapt(65));
- }];
-
- if (self.isNeedSubmitButton) {
- [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.mas_centerX);
- make.top.equalTo(self.recordBtn.mas_bottom).offset(adapt(15));
- make.bottom.equalTo(self).offset(adapt(-10)).priorityHigh();
- }];
-
- [self.rerecordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(self.recordBtn.mas_centerY);
- make.right.equalTo(self.recordBtn.mas_left).offset(adapt(-30));
- make.size.mas_equalTo(self.operationSize);
- }];
-
- [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerY.equalTo(self.recordBtn.mas_centerY);
- make.left.equalTo(self.recordBtn.mas_right).offset(adapt(30));
- make.size.mas_equalTo(self.operationSize);
- }];
- } else {
-
- [self.descLb mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.mas_centerX);
- make.top.equalTo(self.recordBtn.mas_bottom).offset(adapt(15));
- }];
-
- [self.rerecordBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.mas_centerX);
- make.top.equalTo(self.descLb.mas_bottom).offset(adapt(20));
- make.bottom.equalTo(self).offset(adapt(-10)).priorityHigh();
- make.size.mas_equalTo(self.operationSize);
- }];
-
- [self.submitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
- make.centerX.equalTo(self.mas_centerX);
- make.top.equalTo(self.descLb.mas_bottom).offset(adapt(20));
- make.bottom.equalTo(self).offset(adapt(-10)).priorityHigh();
- make.size.mas_equalTo(self.operationSize);
- }];
- }
- [super updateConstraints];
- }
- /// 录制事件
- - (void)recordAction{
- if(self.recordSoundStatusType == YMRecordSoundStatusTypeNone){
- self.recordSoundStatusType = YMRecordSoundStatusTypeStart;
- //创建录音通道
- AVAudioSession *session = [AVAudioSession sharedInstance];
- NSError *sessionError;
- [session setCategory:AVAudioSessionCategoryPlayAndRecord error:&sessionError];
- if(session == nil){
- NSLog(@"Error creating session: %@", [sessionError description]);
- }else{
- [session setActive:YES error:nil];
- }
- // 开始录音
- [self.audioRecorder record];
-
- self.audioTime = 0;
- [self startTimer];
-
- }else if(self.recordSoundStatusType == YMRecordSoundStatusTypeFinish){
- self.recordSoundStatusType = YMRecordSoundStatusTypePlay;
-
- UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
- AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
-
- AVAudioSession * session = [AVAudioSession sharedInstance];
- [session setActive:YES error:nil];
- [session setCategory:AVAudioSessionCategoryPlayback error:nil];
-
- NSError *error;
- self.audioPlayer = [[AVAudioPlayer alloc]initWithContentsOfURL:self.recordAudioUrl error:&error];
- if (error) {
- NSLog(@"%@",error);
- }
- self.audioPlayer.delegate = self;
- /// 播放
- [self.audioPlayer play];
- self.playTime = self.audioTime;
- [self startTimer];
- }else if (self.recordSoundStatusType == YMRecordSoundStatusTypePlay){
- self.recordSoundStatusType = YMRecordSoundStatusTypeFinish;
- /// 停止播放
- [self.audioPlayer stop];
- self.audioPlayer = nil;
- [self stopTimer];
- }else if (self.recordSoundStatusType == YMRecordSoundStatusTypeStart){
- self.recordSoundStatusType = YMRecordSoundStatusTypeFinish;
- /// 停止录制
- [self.audioRecorder stop];
- self.audioRecorder = nil;
- [self stopTimer];
- if (self.audioTime < self.minDuration) {
- [ZCHUDHelper showTitle:stringFormat(@"录音不可少于%ld秒",self.minDuration)];
- [self rerecordAction];
- }
- }
- }
- /// 重录事件
- - (void)rerecordAction{
- [self stopTimer];
-
- self.recordSoundStatusType = YMRecordSoundStatusTypeNone;
-
- [self.audioPlayer stop];
- self.audioPlayer = nil;
- self.audioRecorder = nil;
- self.recordAudioUrl = nil;
-
- if (self.exportUrlBlock) {
- self.exportUrlBlock(nil,0);
- }
- }
- /// 提交事件
- - (void)submitAction{
- if (self.exportUrlBlock) {
- self.exportUrlBlock([self getSubmitSoundUrl], self.audioTime);
- }
- }
- - (void)setMinDuration:(NSInteger)minDuration{
- _minDuration = minDuration;
- }
- - (void)setMaxDuration:(NSInteger)maxDuration{
- _maxDuration = maxDuration;
- }
- - (void)setRerecordBgColor:(UIColor *)rerecordBgColor{
- _rerecordBgColor = rerecordBgColor;
- _rerecordBtn.backgroundColor = rerecordBgColor;
- }
- - (void)setSubmitBgColor:(UIColor *)submitBgColor{
- _submitBgColor = submitBgColor;
- self.submitBtn.backgroundColor = submitBgColor;
- }
- - (void)setRerecordTitleColor:(UIColor *)rerecordTitleColor{
- _rerecordTitleColor = rerecordTitleColor;
- [self.rerecordBtn setTitleColor:rerecordTitleColor forState:UIControlStateNormal];
- }
- - (void)setSubmitTitleColor:(UIColor *)submitTitleColor{
- _submitTitleColor = submitTitleColor;
- [self.submitBtn setTitleColor:submitTitleColor forState:UIControlStateNormal];
- }
- - (void)setRerecordFont:(UIFont *)rerecordFont{
- _rerecordFont = rerecordFont;
- self.rerecordBtn.titleLabel.font = rerecordFont;
- }
- - (void)setSubmitFont:(UIFont *)submitFont{
- _submitFont = submitFont;
- self.submitBtn.titleLabel.font = submitFont;
- }
- - (void)setRerecordTitle:(NSString *)rerecordTitle{
- _rerecordTitle = rerecordTitle;
- [self.rerecordBtn setTitle:rerecordTitle forState:UIControlStateNormal];
- }
- - (void)setSubmitTitle:(NSString *)submitTitle{
- _submitTitle = submitTitle;
- [self.submitBtn setTitle:submitTitle forState:UIControlStateNormal];
- }
- - (void)setRerecordadius:(CGFloat)rerecordRadius{
- _rerecordRadius = rerecordRadius;
- self.rerecordBtn.layer.cornerRadius = rerecordRadius;
- }
- - (void)setSubmitRadius:(CGFloat)submitRadius{
- _submitRadius = submitRadius;
- self.submitBtn.layer.cornerRadius = submitRadius;
- }
- - (void)setRerecordBorderColor:(UIColor *)rerecordBorderColor{
- _rerecordBorderColor = rerecordBorderColor;
- self.rerecordBtn.layer.borderColor = rerecordBorderColor.CGColor;
- }
- - (void)setSubmitBorderColor:(UIColor *)submitBorderColor{
- _submitBorderColor = submitBorderColor;
- self.submitBtn.layer.borderColor = submitBorderColor.CGColor;
- }
- - (void)setRerecordBorderWidth:(CGFloat)rerecordBorderWidth{
- _rerecordBorderWidth = rerecordBorderWidth;
- self.rerecordBtn.layer.borderWidth = rerecordBorderWidth;
- }
- - (void)setSubmitBorderWidth:(CGFloat)submitBorderWidth{
- _submitBorderWidth = submitBorderWidth;
- self.submitBtn.layer.borderWidth = submitBorderWidth;
- }
- - (void)setRerecordImageStr:(NSString *)rerecordImageStr{
- _rerecordImageStr = rerecordImageStr;
- [self.rerecordBtn ym_imageLocationStyle:YMImageLocationStyleTop spacing:adapt(5) imageLocationBlock:^(UIButton * _Nonnull button) {
- [button setTitle:self.rerecordTitle forState:UIControlStateNormal];
- [button setImage:ImageByName(rerecordImageStr) forState:UIControlStateNormal];
- }];
- }
- - (void)setSubmitImageStr:(NSString *)submitImageStr{
- _submitImageStr = submitImageStr;
- [self.submitBtn ym_imageLocationStyle:YMImageLocationStyleTop spacing:adapt(5) imageLocationBlock:^(UIButton * _Nonnull button) {
- [button setTitle:self.submitTitle forState:UIControlStateNormal];
- [button setImage:ImageByName(submitImageStr) forState:UIControlStateNormal];
- }];
- }
- - (void)setRecordSoundStatusType:(YMRecordSoundStatusType)recordSoundStatusType{
- _recordSoundStatusType = recordSoundStatusType;
-
- NSString *recordDuration = @"";
- NSString *desc = @"";
- self.circleView.progress = 0.0f;
- self.circleView.hidden = YES;
- self.rerecordBtn.hidden = YES;
- self.submitBtn.hidden = YES;
-
- UIImage *recordImage = nil;
- switch (recordSoundStatusType) {
- case YMRecordSoundStatusTypeNone:
- {
- recordDuration = @"0s";
- desc = stringFormat(@"录制一段%ld~%ld秒的语音",self.minDuration,self.maxDuration);
- recordImage = ImageByName(@"ym_common_record_sound_none");
- }
- break;
- case YMRecordSoundStatusTypeStart:
- {
- recordDuration = @"0s";
- desc = @"点击结束录音";
- recordImage = ImageByName(@"ym_common_record_sound_start");
- self.circleView.hidden = NO;
- [self progressAnimation];
- }
- break;
- case YMRecordSoundStatusTypeFinish:
- {
- recordDuration = stringFormat(@"%lds",self.audioTime);
- desc = @"点击试听";
- recordImage = ImageByName(@"ym_common_record_sound_finish");
- self.rerecordBtn.hidden = NO;
- if (self.isNeedSubmitButton) {
- self.submitBtn.hidden = NO;
- } else {
- self.submitBtn.hidden = YES;
- }
- }
- break;
- case YMRecordSoundStatusTypePlay:
- {
- recordDuration = [NSString stringWithFormat:@"%@s",@(self.audioTime)];
- desc = @"播放中";
- recordImage = ImageByName(@"ym_common_record_sound_play");
- self.rerecordBtn.hidden = NO;
- if (self.isNeedSubmitButton) {
- self.submitBtn.hidden = NO;
- } else {
- self.submitBtn.hidden = YES;
- }
- }
- break;
- case YMRecordSoundStatusTypeSubmit:
- {
-
- }
- break;
- default:
- break;
- }
- self.recordDurationLb.text = recordDuration;
- self.descLb.text = desc;
- [self.recordBtn setBackgroundImage:recordImage forState:UIControlStateNormal];
- }
- #pragma mark - AVAudioPlayerDelegate
- - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag{
- self.recordSoundStatusType = YMRecordSoundStatusTypeFinish;
- // 停止播放
- [self.audioPlayer stop];
- self.audioPlayer = nil;
- [self stopTimer];
- }
- - (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error{
- self.recordSoundStatusType = YMRecordSoundStatusTypeFinish;
- // 停止播放
- [self.audioPlayer stop];
- self.audioPlayer = nil;
- [self stopTimer];
- }
- - (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player{
- self.recordSoundStatusType = YMRecordSoundStatusTypeFinish;
- // 停止播放
- [self.audioPlayer stop];
- self.audioPlayer = nil;
- [self stopTimer];
- }
- #pragma mark - AVAudioRecorderDelegate
- - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
-
- if (!self.isNeedSubmitButton) {
- [self submitAction];
- }
- }
- - (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError * _Nullable)error{
- self.recordSoundStatusType = YMRecordSoundStatusTypeFinish;
- // 停止录制
- [self.audioRecorder stop];
- self.audioRecorder = nil;
- [self stopTimer];
-
- }
- - (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{
- self.recordSoundStatusType = YMRecordSoundStatusTypeFinish;
- // 停止录制
- [self.audioRecorder stop];
- self.audioRecorder = nil;
- [self stopTimer];
-
- }
- // 开始计时
- - (void)startTimer{
- self.recordTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
- [[NSRunLoop currentRunLoop] addTimer:self.recordTimer forMode:NSRunLoopCommonModes];
- }
- // 结束计时
- - (void)stopTimer{
- [self.recordTimer invalidate];
- self.recordTimer = nil;
- self.recordDurationLb.text = stringFormat(@"%lds",self.audioTime);
- }
- // 进度动画
- - (void)progressAnimation{
- __block CGFloat timeout = 0.0f;
- dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
- dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
- dispatch_source_set_timer(_timer, DISPATCH_TIME_NOW, 0.025f * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
- //3.设置定时器要执行的任务
- dispatch_source_set_event_handler(_timer, ^{
- if(timeout >= 1.0f){
- dispatch_source_cancel(_timer);
- dispatch_async(dispatch_get_main_queue(), ^{
-
- });
- }else{
- timeout += 0.025f;
- CGFloat currentTime = (CGFloat)self.audioTime + timeout;
- dispatch_async(dispatch_get_main_queue(), ^{
- self.circleView.progress = currentTime / (CGFloat)self.maxDuration;
- });
- }
- });
- dispatch_resume(_timer);
- }
- - (void)timerAction{
- if (self.recordSoundStatusType == YMRecordSoundStatusTypeStart) {
- self.audioTime++;
- self.recordDurationLb.text = stringFormat(@"%lds",self.audioTime);
- [self progressAnimation];
- if (self.audioTime >= self.maxDuration) {
- self.recordSoundStatusType = YMRecordSoundStatusTypeFinish;
- // 停止录制
- [self.audioRecorder stop];
- self.audioRecorder = nil;
- [self stopTimer];
- }
- }else if (self.recordSoundStatusType == YMRecordSoundStatusTypePlay){
- self.playTime--;
- self.recordDurationLb.text = stringFormat(@"%lds",self.playTime);
- }
- }
- - (NSURL *)recordAudioUrl{
- if (!_recordAudioUrl) {
- NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) lastObject];
- NSString *fileName;
- if (self.exportType == YMRecordSoundExportTypeAAC) {
- fileName = @"audioVerify.aac";
- }else{
- fileName = @"audioVerify.caf";
- }
- _recordAudioUrl = [NSURL fileURLWithPath:[path stringByAppendingPathComponent:fileName]];
- if ([[NSFileManager defaultManager] fileExistsAtPath:_recordAudioUrl.resourceSpecifier]){
- [[NSFileManager defaultManager] removeItemAtPath:_recordAudioUrl.resourceSpecifier error:nil];
- }
- }
- return _recordAudioUrl;
- }
- - (NSURL*)getSubmitSoundUrl{
-
- [self stopTimer];
- // 停止播放
- [self.audioPlayer stop];
- if (_recordSoundStatusType < YMRecordSoundStatusTypeFinish){
- return nil;
- }
- NSURL *url = nil;
- switch (self.exportType) {
- case YMRecordSoundExportTypePM3:
- url = [self.recordAudioUrl transformCAFToMP3];
- break;
- case YMRecordSoundExportTypeAAC:{
- url = self.recordAudioUrl;
- }
- break;
- default:
- break;
- }
-
- return url;
- }
- - (void)stopRecord{
- [self stopTimer];
- // 停止播放
- [self.audioPlayer stop];
- self.audioPlayer = nil;
- self.audioRecorder = nil;
- self.recordAudioUrl = nil;
- }
- - (void)dealloc{
- [self stopRecord];
- }
- - (UILabel *)recordDurationLb{
- if (!_recordDurationLb) {
- _recordDurationLb = [[UILabel alloc]init];
- _recordDurationLb.font = LCFont(12);
- _recordDurationLb.textColor = UIColor.blackColor;
- _recordDurationLb.textAlignment = NSTextAlignmentCenter;
- _recordDurationLb.text = @"0s";
- }
- return _recordDurationLb;
- }
- - (YMCircleView *)circleView{
- if (!_circleView) {
- _circleView = [[YMCircleView alloc]init];
- _circleView.progress = 0.0f;
- _circleView.progerssColor = HexColorFromRGB(0xFDD45E);
- _circleView.progerssBackgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0.13f];
- _circleView.progerWidth = 2.5f;
- _circleView.progerBackgroundWidth = 1.0f;
- _circleView.hidden = YES;
- }
- return _circleView;
- }
- - (UIButton *)recordBtn{
- if (!_recordBtn) {
- _recordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- WS(weakSelf)
- [[[_recordBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
-
- // 检查权限
- [UCAuthorityManager microPhoneAuthority:^{
- [weakSelf recordAction];
- } denied:^{
-
- YMTipsPopupView *customView = [[YMTipsPopupView alloc]init];
- [customView configutationWithTips:@"请在iphone的“设置-隐私-麦克风”选项中,允许APP访问您的麦克风。" TipsAlignment:NSTextAlignmentCenter IsHideTitle:NO IsHideSingleButton:NO];
- YMPopupView *popupView = [YMPopupView initWithCustomView:customView parentView:[YMGlobalUtils getCurrentVC].view popStyle:YMPopupStyleFade dismissStyle:YMDismissStyleFade];
- popupView.priority = 999;
- popupView.cornerRadius = adapt(10);
- popupView.rectCorners = UIRectCornerAllCorners;
- popupView.positionStyle = YMPositionStyleCenter;
- popupView.isHideBg = NO;
- popupView.bgAlpha = 0.3;
- @weakify(popupView)
- customView.buttonBlock = ^(BOOL issubmit) {
- @strongify(popupView)
- if (issubmit) {
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
- }
- [popupView dismissWithStyle:YMDismissStyleFade duration:2.0];
- };
- [popupView pop];
-
- }];
- }];
- }
- return _recordBtn;
- }
- - (UILabel *)descLb{
- if (!_descLb) {
- _descLb = [[UILabel alloc]init];
- _descLb.font = LCFont(12);
- _descLb.textColor = HexColorFromRGB(0x9d9d9d);
- _descLb.textAlignment = NSTextAlignmentCenter;
- _descLb.text = stringFormat(@"录制一段%ld~%ld秒的语音",self.minDuration,self.maxDuration);
- }
- return _descLb;
- }
- - (UIButton *)rerecordBtn{
- if (!_rerecordBtn) {
- _rerecordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _rerecordBtn.backgroundColor = self.rerecordBgColor;
- _rerecordBtn.titleLabel.font = self.rerecordFont;
- [_rerecordBtn setTitleColor:self.rerecordTitleColor forState:UIControlStateNormal];
- [_rerecordBtn setTitle:self.rerecordTitle forState:UIControlStateNormal];
- _rerecordBtn.layer.cornerRadius = self.rerecordRadius;
- _rerecordBtn.layer.masksToBounds = YES;
- _rerecordBtn.hidden = YES;
- WS(weakSelf)
- [[[_rerecordBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
- [weakSelf rerecordAction];
- }];
- }
- return _rerecordBtn;
- }
- - (UIButton *)submitBtn{
- if (!_submitBtn) {
- _submitBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- _submitBtn.backgroundColor = self.submitBgColor;
- _submitBtn.titleLabel.font = self.submitFont;
- [_submitBtn setTitleColor:self.submitTitleColor forState:UIControlStateNormal];
- [_submitBtn setTitle:self.submitTitle forState:UIControlStateNormal];
- _submitBtn.layer.cornerRadius = self.submitRadius;
- _submitBtn.layer.masksToBounds = YES;
- _submitBtn.hidden = YES;
- WS(weakSelf)
- [[[_submitBtn rac_signalForControlEvents:UIControlEventTouchUpInside] takeUntil:self.rac_willDeallocSignal] subscribeNext:^(id x) {
- [weakSelf submitAction];
- }];
- }
- return _submitBtn;
- }
- - (AVAudioRecorder *)audioRecorder{
- if (!_audioRecorder) {
- //创建录音机
- NSError *error = nil;
- _audioRecorder = [[AVAudioRecorder alloc] initWithURL:self.recordAudioUrl settings:[self getAudioSetting] error:&error];
- _audioRecorder.delegate = self;
- if (error) {
- NSLog(@"创建录音机对象时发生错误,错误信息:%@",error.localizedDescription);
- return nil;
- }
- }
- return _audioRecorder;
- }
- /**
- * 取得录音文件设置
- *
- * @return 录音设置
- */
- - (NSDictionary *)getAudioSetting{
- if (self.exportType == YMRecordSoundExportTypeAAC) {
- NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc]init] ;
- //设置录音格式 AVFormatIDKey==kAudioFormatLinearPCM
- [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
- //设置录音采样率(Hz) 如:AVSampleRateKey==8000/44100/96000(影响音频的质量)
- [recordSetting setValue:[NSNumber numberWithFloat:44100] forKey:AVSampleRateKey];
- //录音通道数 1 或 2
- [recordSetting setValue:[NSNumber numberWithInt:1] forKey:AVNumberOfChannelsKey];
- //线性采样位数 8、16、24、32
- [recordSetting setValue:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
- //录音的质量
- [recordSetting setValue:[NSNumber numberWithInt:AVAudioQualityMedium] forKey:AVEncoderAudioQualityKey];
- return recordSetting.copy;
- }else{
- NSDictionary *recordSetting = @{
- AVFormatIDKey : @(kAudioFormatLinearPCM),
- AVSampleRateKey : @(44100),
- AVNumberOfChannelsKey : @(2),
- AVLinearPCMBitDepthKey : @(16),
- AVLinearPCMIsNonInterleaved : @NO,
- AVLinearPCMIsFloatKey : @NO,
- AVLinearPCMIsBigEndianKey : @NO,
- };
-
- return recordSetting;
- }
- }
- @end
|