【发布时间】:2012-10-27 08:52:42
【问题描述】:
如何以 iLBC 格式录制音频? 我从 Question #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 );
}
【问题讨论】:
-
好的,通过删除修复它:[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];我猜它们与 iLBC 默认值冲突。
-
请将您的评论转化为答案并接受。
标签: iphone ios xcode format avaudiorecorder