我想要iLBC格式,但似乎只能以IMA4格式录制音频

时间:2012-10-27 08:52:43

标签: iphone ios xcode format avaudiorecorder

如何以iLBC格式录制音频? 我从问题#1010343得到了一些帮助,并实现了这段代码:

NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] initWithCapacity:10];        
[recordSetting setValue:[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
//  [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];  //  doesn't work:  no error, but no sound
//  [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatiLBC] forKey:AVFormatIDKey];  //  doesn't work:  no error, but not sound
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
[recordSetting setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
[recordSetting setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[recordSetting setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];
recorder = [[AVAudioRecorder alloc] initWithURL:audioFile settings:recordSetting error: &avError ];
[recorder setDelegate:self];
[recorder recordForDuration:(NSTimeInterval) secLeft];
[recorder record];

如上所述,它运作正常。好录音。轻松播放。您注意到其他格式已注释掉。如果我尝试其中任何一个,我什么也得不到。我想要更小的文件,并根据问题#7279643,它提供了这个有价值的信息:

Here are the results for few encoding supported by iPhone:
Size of audio file of duration 10 sec.
   kAudioFormatMPEG4AAC :      164 kB
   kAudioFormatAppleLossless : 430 kB
   kAudioFormatAppleIMA4 :     475 kB
   kAudioFormatULaw :          889 kB
   kAudioFormatALaw :          889 KB

我应该可以在iLBC中录制。但如果我尝试,我什么也得不到。当我使用IMA4格式以外的格式时,还需要在录制或播放时更改其他内容吗?

这是我的播放代码:

NSError     *avError;
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayback error:&avError];
[audioSession setActive:YES error:&avError];
if( !avError ) {
    if( [audioPlayer isPlaying] ) {
        [audioPlayer stop ];
    }
    while( ![audioPlayer isPlaying] ) {
        AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFile  error:nil];
        self.audioPlayer = player;
        self.audioPlayer.numberOfLoops = 0;
        [audioPlayer prepareToPlay];
        [audioPlayer play];
    }
} else {
    NSLog(@"Playback Error: %@", avError );
}

1 个答案:

答案 0 :(得分:0)

通过删除来修复它:

[recordSetting setValue:[NSNumber numberWithFloat:44100.0]forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]

我猜它们与iLBC默认值冲突。