简体   繁体   English

iPhone-使用AVAudioRecorder进行录音

[英]iPhone - Voice recording using AVAudioRecorder

I have followed a sample code for the above, everything seems to be working fine without a single error, the voice file (.caf) is created successfully. 我已经遵循了上面的示例代码,一切似乎都正常工作,没有一个错误,语音文件(.caf)已成功创建。

The only problem is the file is always 29kb and without any sound in it. 唯一的问题是文件始终为29kb,并且没有任何声音。

I am running the app in my simulator, not sure if I have missed out anything. 我正在模拟器中运行该应用程序,不确定是否遗漏了任何内容。 Is there anything that I need to set for my simulator to work? 我需要为模拟器进行设置吗?

Below is my code: 下面是我的代码:

-(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");
}

Please advice. 请指教。 Googling up and down and yet can't find the cause of it. 上下搜索,但找不到原因。 Do I need to install soundflower to make it work? 我需要安装Soundflower使其正常工作吗?

Found out that for the audio format of kAudioFormatMPEG4AAC, the AVEncoderBitRateKey cannot be set to 12800. Comment off the bit rate statement and the recording works successfully. 发现对于kAudioFormatMPEG4AAC的音频格式,无法将AVEncoderBitRateKey设置为12800。注释掉比特率语句,录制成功。

Have yet to find out what should be the correct and supported bit rate for kAudioFormatMPEG4AAC, but this is at least the answer of the question, for anybody who runs into the same situation like me. 尚未找到kAudioFormatMPEG4AAC的正确和受支持的比特率应该是什么,但这至少是问题的答案,对于遇到像我一样的情况的任何人来说。

:) :)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM