2011-10-03 63 views
1

我已經按照上面的示例代碼,一切似乎沒有任何錯誤工作正常,語音文件(.caf)已成功創建。iPhone - 使用AVAudioRecorder錄音

唯一的問題是該文件總是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才能使其工作?

回答

4

發現對於kAudioFormatMPEG4AAC的音頻格式,AVEncoderBitRateKey不能設置爲12800.註釋掉比特率聲明並且錄製成功。

還沒有找出kAudioFormatMPEG4AAC的正確和支持的比特率是什麼,但這至少是問題的答案,對於遇到像我一樣的情況的任何人來說。

:)

+0

是的它現在真的幫了我。謝謝.. –

1

這是除了你的編碼比特率都好應該是128000(128K)不是12800(12.8k)。

相關問題