修复bug

This commit is contained in:
liyuhua
2024-03-04 14:14:33 +08:00
parent 268a80f27b
commit 5eddd09197
25 changed files with 5991 additions and 4825 deletions

View File

@@ -10,17 +10,16 @@
NS_ASSUME_NONNULL_BEGIN
@interface RecordVoiceManager : NSObject
/// 声音录制对象
@property (nonatomic, strong) AVAudioRecorder *audioRecorder;
/// 本地录制的音频地址
@property (nonatomic, copy) NSString *totalFilePath;
/// 音频文件名
@property (nonatomic, copy) NSString *fileName;
///播放器4
@property (nonatomic, strong) AVAudioRecorder *recorder;
@property (nonatomic, copy) NSString *path;
@property (nonatomic, copy) NSString *file;
@property (nonatomic, strong) AVAudioPlayer *player;
/// 是否正在播放声音
@property (nonatomic, assign) BOOL isPlayVoice;
/// 初始化录音对象
- (void)initWithRecord;
-(void)stopRecord;
- (void)playFilePathVoice;

View File

@@ -9,8 +9,8 @@
@implementation RecordVoiceManager
-(void)stopRecord{
[self.audioRecorder stop];
self.audioRecorder = nil;
[self.recorder stop];
self.recorder = nil;
}
///
- (void)initWithRecord {
@@ -18,47 +18,38 @@
[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];
self.file = [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];
self.path = [filePath stringByAppendingPathComponent:self.file];
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:@"录制失败,请重试"];
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) {
return;
}
if(![self.audioRecorder prepareToRecord]) {
// [MewHUDTool showErrorWithMessage:@"录制失败,请重试"];
if(![self.recorder prepareToRecord]) {
return;
}
BOOL status = [self.audioRecorder record];
BOOL status = [self.recorder record];
NSLog(@"%d",status);
}
- (void)playFilePathVoice {
@@ -70,10 +61,10 @@
}
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionAllowBluetooth error:nil];
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:self.totalFilePath];
BOOL isFile = [[NSFileManager defaultManager] fileExistsAtPath:self.path];
NSError *AVerror = NULL;
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.totalFilePath] error:&AVerror];
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:self.path] error:&AVerror];
self.player.volume = 1;
self.player.numberOfLoops = -1;