iOS - AVAudioRecorder - 使用ADPCM或IMA录制

时间:2013-09-16 20:17:26

标签: avaudiorecorder avaudiosession adpcm

使用这些录制设置,我可以让AVAudioRecorder工作:

    recordSetting = [NSDictionary dictionaryWithObjectsAndKeys:
//                     [NSNumber numberWithFloat:2048.0f],AVSampleRateKey,
//                     [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
//                     [NSNumber numberWithInt:8],AVLinearPCMBitDepthKey,
                     [NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,
//                     [NSNumber numberWithBool:NO], AVLinearPCMIsFloatKey,
//                     [NSNumber numberWithBool:0], AVLinearPCMIsBigEndianKey,
//                     [NSNumber numberWithBool:NO], AVLinearPCMIsNonInterleaved,
//                     [NSNumber numberWithInt:256], AVEncoderBitRateKey,
                     [NSData data], AVChannelLayoutKey, nil];


recorder = [[ AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&err];


if(!recorder){
    NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
    UIAlertView *alert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: [err localizedDescription]
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [alert show];

    return;
}

一改变一下:

[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,

到此:

[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,

我收到以下错误:

recorder: NSOSStatusErrorDomain 1718449215 {
}

也就是说,不支持音频格式。 kAudioFormatUnsupportedDataFormatError

如何让ADPCM或IMA与AVAudioRecorder配合使用?

1 个答案:

答案 0 :(得分:0)

1)请确保为录制(或)播放(或)两者(即VOIP)正确设置会话,如下所示:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];

2)使用以下代码,因为这对我来说很好(我压缩到低质量,因为我有一个消息应用程序):

NSDictionary *AudioSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                               [NSNumber numberWithInt:kAudioFormatAppleIMA4], AVFormatIDKey
                               , [NSNumber numberWithFloat:11025.00], AVSampleRateKey
                               , [NSNumber numberWithInt:1], AVNumberOfChannelsKey
                               , [NSNumber numberWithInt:11025], AVEncoderBitRateKey
                               , [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey
                               , [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey
                               , [NSNumber numberWithInt:1], AVNumberOfChannelsKey
                               , nil
                               ];

此致 Heider Sati