Files
yingmeng-ios-switf/yinmeng-ios/Base/Security/RecordVoiceManager.m

86 lines
3.4 KiB
Mathematica
Raw Normal View History

2024-03-01 16:54:46 +08:00
//
// RecordVoiceManager.m
// yinmeng-ios
//
// Created by duoban on 2024/3/1.
//
#import "RecordVoiceManager.h"
@implementation RecordVoiceManager
-(void)stopRecord{
[self.audioRecorder stop];
self.audioRecorder = nil;
}
///
- (void)initWithRecord {
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
//
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] init];
//
[recordSettings setValue :[NSNumber numberWithInt:kAudioFormatLinearPCM] forKey: AVFormatIDKey];
//
[recordSettings setValue :[NSNumber numberWithFloat:16000.0] forKey: AVSampleRateKey];
//
[recordSettings setValue :[NSNumber numberWithInt:2] forKey: AVNumberOfChannelsKey];
//线
[recordSettings setValue :[NSNumber numberWithInt:16] forKey: AVLinearPCMBitDepthKey];
//,
[recordSettings setValue:[NSNumber numberWithInt:AVAudioQualityMin] forKey:AVEncoderAudioQualityKey];
NSError *error = nil;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *date = [dateFormat stringFromDate:[NSDate date]];
self.fileName = [NSString stringWithFormat:@"voiceShow_%@.wav", date];
NSFileManager *fileMgr = [[NSFileManager alloc] init];
//
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"voiceShow"];
self.totalFilePath = [filePath stringByAppendingPathComponent:self.fileName];
if (![fileMgr fileExistsAtPath:filePath]) {
//,
[fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
if (![fileMgr fileExistsAtPath:filePath]) { return; }
NSURL *fileUrl = [NSURL fileURLWithPath:self.totalFilePath];
self.audioRecorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSettings error:&error];
self.audioRecorder.meteringEnabled = YES;
[self.audioRecorder updateMeters];
NSLog(@"%f",[self.audioRecorder peakPowerForChannel:0]);
if(!self.audioRecorder) {
// [MewHUDTool showErrorWithMessage:@"录制失败,请重试"];
return;
}
if(![self.audioRecorder prepareToRecord]) {
// [MewHUDTool showErrorWithMessage:@"录制失败,请重试"];
return;
}
BOOL status = [self.audioRecorder record];
NSLog(@"%d",status);
}
- (void)playFilePathVoice {
if (self.isPlayVoice) {
[self.player stop];
self.player = nil;
self.isPlayVoice = NO;
return;
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:self.totalFilePath];
NSError *AVerror = NULL;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.totalFilePath] error:&AVerror];
self.player.volume = 1;
self.player.numberOfLoops = -1;
[self.player prepareToPlay];
[self.player play];
self.isPlayVoice = YES;
}
@end