iPhone - 使用AVAudioRecorder进行录音

时间:2011-10-03 23:31:42

标签: iphone avaudiorecorder

我已经按照上面的示例代码,一切似乎工作正常,没有一个错误,语音文件(.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才能使其正常工作?

2 个答案:

答案 0 :(得分:4)

发现对于kAudioFormatMPEG4AAC的音频格式,AVEncoderBitRateKey不能设置为12800.注释比特率语句并且录制成功。

尚未找出kAudioFormatMPEG4AAC的正确和支持的比特率应该是什么,但对于遇到像我这样的情况的人来说,这至少是问题的答案。

:)

答案 1 :(得分:1)

除了你的编码器比特率应该是128000(128k)而不是12800(12.8k)之外,这一切都很好。