修复录音文件长度为0的问题
This commit is contained in:
@@ -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]);
|
||||
}
|
||||
|
||||
// 修复:使用fileURLWithPath创建正确的文件URL
|
||||
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];
|
||||
|
||||
Reference in New Issue
Block a user