修复录音文件长度为0的问题

This commit is contained in:
joywayer
2025-06-14 22:55:43 +08:00
parent 715e08f787
commit bba3ed1cb4
3 changed files with 343 additions and 207 deletions

View File

@@ -69,8 +69,6 @@
}
return bCanRecord;
}
//-(int)ifauth
@@ -102,32 +100,86 @@
#pragma mark -
- (void)beginRecordByFileName:(NSString*)_fileName;{
NSLog(@"开始初始化录音,文件名: %@", _fileName);
//
self.recordFileName = _fileName;
self.recordFilePath = [VoiceRecorderBaseVC getPathByFileName:recordFileName ofType:@"wav"];
NSLog(@"录音将保存到路径: %@", recordFilePath);
//
NSString *dirPath = [recordFilePath stringByDeletingLastPathComponent];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dirPath]) {
NSError *dirError = nil;
[fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&dirError];
if (dirError) {
NSLog(@"创建录音目录失败: %@", [dirError localizedDescription]);
}
}
//
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *sessionError = nil;
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord
withOptions:AVAudioSessionCategoryOptionDefaultToSpeaker
error:&sessionError];
if (sessionError) {
NSLog(@"设置音频会话失败: %@", [sessionError localizedDescription]);
}
BOOL audioSessionActive = [audioSession setActive:YES error:&sessionError];
if (!audioSessionActive) {
NSLog(@"激活音频会话失败: %@", [sessionError localizedDescription]);
}
// 使fileURLWithPathURL
NSError *recError = nil;
NSURL *fileURL = [NSURL fileURLWithPath:recordFilePath];
NSLog(@"文件URL: %@", [fileURL absoluteString]);
//
if ([fileManager fileExistsAtPath:recordFilePath]) {
[fileManager removeItemAtPath:recordFilePath error:nil];
NSLog(@"已删除旧的录音文件");
}
//
NSDictionary *recordSettings = [VoiceRecorderBaseVC getAudioRecorderSettingDict];
//
self.recorder = [[[AVAudioRecorder alloc]initWithURL:[NSURL URLWithString:recordFilePath]
settings:[VoiceRecorderBaseVC getAudioRecorderSettingDict]
error:nil]autorelease];
self.recorder = [[[AVAudioRecorder alloc] initWithURL:fileURL
settings:recordSettings
error:&recError] autorelease];
if (recError) {
NSLog(@"录音初始化失败: %@", [recError localizedDescription]);
return;
}
recorder.delegate = self;
recorder.meteringEnabled = YES;
[recorder prepareToRecord];
//
BOOL prepareSuccess = [recorder prepareToRecord];
if (!prepareSuccess) {
NSLog(@"录音准备失败");
return;
}
//
curCount = 0;
//
canNotSend = NO;
//
[[AVAudioSession sharedInstance] setCategory: AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];
[recorder record];
//
BOOL recordSuccess = [recorder record];
if (!recordSuccess) {
NSLog(@"开始录音失败");
return;
}
NSLog(@"开始录音成功, 录音状态: %d", recorder.isRecording);
//
[self startTimer];
@@ -167,12 +219,16 @@
}
#pragma mark -
- (void)updateMeters{
if (recorder.isRecording){
if (recorder && recorder.isRecording){
//
[recorder updateMeters];
[recorderView updateMetersByAvgPower:[recorder averagePowerForChannel:0]];
// NSLog(@"峰值:%f",[recorder averagePowerForChannel:0]);
float avgPower = [recorder averagePowerForChannel:0];
[recorderView updateMetersByAvgPower:avgPower];
//
if (fmod(curCount, 1.0) < 0.1) {
NSLog(@"录音中... 时长: %.1f秒, 峰值: %.2f", curCount, avgPower);
}
//
if (curCount >= maxRecordTime - 10 && curCount < maxRecordTime) {
@@ -246,14 +302,25 @@
//
[self stopTimer];
NSLog(@"触摸结束,录音结束,当前录音时长: %.1f秒", curCount);
curTouchPoint = CGPointZero;
[self removeScreenTouchObserver];
[UIView hideViewByCompletion:^(BOOL finish){
//
if (recorder.isRecording)
if (recorder && recorder.isRecording) {
NSLog(@"停止录音");
[recorder stop];
//
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:recordFilePath]) {
NSDictionary *attrs = [fileManager attributesOfItemAtPath:recordFilePath error:nil];
NSLog(@"最终录音文件大小: %@ 字节", [attrs objectForKey:NSFileSize]);
}
}
if (canNotSend) {
//
@@ -269,11 +336,22 @@
#pragma mark - AVAudioRecorder Delegate Methods
- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSLog(@"录音停止");
NSLog(@"录音完成回调,成功: %d", flag);
if (!flag) {
NSLog(@"录音完成但不成功!");
}
[self stopTimer];
curCount = 0;
}
- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError *)error {
NSLog(@"录音编码错误: %@", [error localizedDescription]);
[self stopTimer];
curCount = 0;
}
- (void)audioRecorderBeginInterruption:(AVAudioRecorder *)recorder{
NSLog(@"录音开始");
[self stopTimer];