简体   繁体   中英

iOS - AVAudioRecorder - recording with ADPCM or IMA

USing these recording settings, I am able to get the AVAudioRecorder to work:

    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;
}

As soon as I change this:

[NSNumber numberWithInt:kAudioFormatLinearPCM],AVFormatIDKey,

to this:

[NSNumber numberWithInt:kAudioFormatAppleIMA4],AVFormatIDKey,

I get the following error:

recorder: NSOSStatusErrorDomain 1718449215 {
}

Which is to say, the audio format is not supported. kAudioFormatUnsupportedDataFormatError

What do I need to do to get ADPCM or IMA to work with AVAudioRecorder?

1) Please ensure that you set the session correctly for either Recording (or) Playback (or) Both (ie VOIP) as follows:

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

2) Use the following code, as this works fine for me (I compress to low quality as I have a messaging app):

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
                               ];

Regards Heider Sati

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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