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

77 lines
2.9 KiB
Mathematica
Raw Normal View History

2024-03-01 16:54:46 +08:00
//
// RecordVoiceManager.m
// yinmeng-ios
//
2024-03-05 14:04:09 +08:00
// Created by yinmeng on 2024/3/1.
2024-03-01 16:54:46 +08:00
//
#import "RecordVoiceManager.h"
@implementation RecordVoiceManager
-(void)stopRecord{
2024-03-04 14:14:33 +08:00
[self.recorder stop];
self.recorder = nil;
2024-03-01 16:54:46 +08:00
}
///
- (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]];
2024-03-04 14:14:33 +08:00
self.file = [NSString stringWithFormat:@"voiceShow_%@.wav", date];
2024-03-01 16:54:46 +08:00
NSFileManager *fileMgr = [[NSFileManager alloc] init];
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"voiceShow"];
2024-03-04 14:14:33 +08:00
self.path = [filePath stringByAppendingPathComponent:self.file];
2024-03-01 16:54:46 +08:00
if (![fileMgr fileExistsAtPath:filePath]) {
[fileMgr createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
}
if (![fileMgr fileExistsAtPath:filePath]) { return; }
2024-03-04 14:14:33 +08:00
NSURL *fileUrl = [NSURL fileURLWithPath:self.path];
self.recorder = [[AVAudioRecorder alloc] initWithURL:fileUrl settings:recordSettings error:&error];
self.recorder.meteringEnabled = YES;
[self.recorder updateMeters];
NSLog(@"%f",[self.recorder peakPowerForChannel:0]);
if(!self.recorder) {
2024-03-01 16:54:46 +08:00
return;
}
2024-03-04 14:14:33 +08:00
if(![self.recorder prepareToRecord]) {
2024-03-01 16:54:46 +08:00
return;
}
2024-03-04 14:14:33 +08:00
BOOL status = [self.recorder record];
2024-03-01 16:54:46 +08:00
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];
2024-03-04 14:14:33 +08:00
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:self.path];
2024-03-01 16:54:46 +08:00
NSError *AVerror = NULL;
2024-03-04 14:14:33 +08:00
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.path] error:&AVerror];
2024-03-01 16:54:46 +08:00
self.player.volume = 1;
self.player.numberOfLoops = -1;
[self.player prepareToPlay];
[self.player play];
self.isPlayVoice = YES;
}
@end