iPhone - 使用 AVAudioRecorder 录音
Posted
技术标签:
【中文标题】iPhone - 使用 AVAudioRecorder 录音【英文标题】:iPhone - Voice recording using AVAudioRecorder 【发布时间】:2011-10-03 23:31:42 【问题描述】:我已经按照上面的示例代码,一切似乎都工作正常,没有一个错误,语音文件 (.caf) 已成功创建。
唯一的问题是文件总是 29kb 并且没有任何声音。
我正在我的模拟器中运行该应用程序,不确定我是否错过了任何东西。我需要设置什么才能让我的模拟器正常工作吗?
下面是我的代码:
-(IBAction) startRecording
NSLog(@"startRecording");
[audioRecorder release];
audioRecorder = nil;
// Init audio with record capability
AVAudiosession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryRecord error:nil];
NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];
if(recordEncoding == ENC_PCM)
[recordSettings setObject:[NSNumber numberWithInt: kAudioFormatLinearPCM] forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
[recordSettings setObject:[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey];
else
NSNumber *formatObject;
switch (recordEncoding)
case (ENC_AAC):
formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];
break;
case (ENC_ALAC):
formatObject = [NSNumber numberWithInt: kAudioFormatAppleLossless];
break;
case (ENC_IMA4):
formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
break;
case (ENC_ILBC):
formatObject = [NSNumber numberWithInt: kAudioFormatiLBC];
break;
case (ENC_ULAW):
formatObject = [NSNumber numberWithInt: kAudioFormatULaw];
break;
default:
formatObject = [NSNumber numberWithInt: kAudioFormatAppleIMA4];
[recordSettings setObject:formatObject forKey: AVFormatIDKey];
[recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
[recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
[recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *sourceFilename = [[NSString alloc] initWithString:@"r.caf"];
NSString *sourcePath = [documentsDirectory stringByAppendingPathComponent:sourceFilename];
[sourceFilename release];
//[[NSFileManager defaultManager] createFileAtPath:sourcePath contents:nil attributes:nil];
NSURL *url = [NSURL fileURLWithPath:sourcePath];
NSError *error = nil;
audioRecorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSettings error:&error];
audioRecorder.delegate = self;
if(!audioRecorder)
NSLog(@"recorder: %@ %d %@", [error domain], [error code], [[error userInfo] description]);
UIAlertView *alert =
[[UIAlertView alloc] initWithTitle: @"Warning"
message: [error localizedDescription]
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
return;
if ([audioRecorder prepareToRecord] == YES)
[audioRecorder record];
else
int errorCode = CFSwapInt32HostToBig ([error code]);
NSLog(@"Error: %@ [%4.4s])" , [error localizedDescription], (char*)&errorCode);
NSLog(@"recording");
请指教。谷歌搜索上下,但无法找到它的原因。我是否需要安装 soundflower 才能使其工作?
【问题讨论】:
【参考方案1】:发现kAudioFormatMPEG4AAC的音频格式,AVEncoderBitRateKey不能设置为12800,注释掉比特率语句,录音成功。
尚未确定 kAudioFormatMPEG4AAC 的正确和支持的比特率应该是多少,但这至少是问题的答案,对于像我这样遇到相同情况的任何人。
:)
【讨论】:
【参考方案2】:这一切都很好,只是您的编码器比特率应该是 128000 (128k) 而不是 12800 (12.8k)。
【讨论】:
以上是关于iPhone - 使用 AVAudioRecorder 录音的主要内容,如果未能解决你的问题,请参考以下文章
iPhone:如何使用 AVAudioPlayer 检测 Iphone/Ipod 音量级别
更新为 iPhone 5 制作的 XIB 以与 iPhone 6 和 iPhone 6 Plus 一起使用
Iphone - 不推荐使用的功能......他们会让iphone崩溃吗?