I want iLBC format, but I can only record audio in IMA4 format

How to record audio in iLBC format? I got some help from Question # 1010343 and implemented this code:

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

As above, it works fine. Good record. Simple reproduction. You notice that other formats are commented out. If I try any of them, I won’t get anything. I want smaller files, and according to Question # 7279643 , which gives this valuable information:

 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 

I must be able to record in iLBC. But if I try, I won’t get anything. Is there anything else I need to change when recording or playing back when I use a format other than IMA4?

Here is my replay code:

 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 ); } 
+4
source share
1 answer

Fixed by deleting:

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

I assume they conflict with iLBC defaults.

0
source

Source: https://habr.com/ru/post/1442457/


All Articles