iOS:audio录制文件格式

我正在实施audio录制。 它适用于cafwave格式。 但问题是文件大小太大。

所以,任何人都可以帮助我loggingaudio格式,也发挥在窗口和文件大小是低的。

我试过的代码如下:

  dirPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0]; NSString *soundFilePath = [docsDir stringByAppendingPathComponent:@"sound.wave"]; NSLog(@"%@",soundFilePath); NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath]; NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt:AVAudioQualityMin], AVEncoderAudioQualityKey, [NSNumber numberWithInt:16], AVEncoderBitRateKey, [NSNumber numberWithInt: 2], AVNumberOfChannelsKey, [NSNumber numberWithFloat:8000.0], AVSampleRateKey, [NSNumber numberWithInt:8], AVLinearPCMBitDepthKey, nil]; NSError *error = nil; audioRecorder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:recordSettings error:&error]; if (error) { NSLog(@"error: %@", [error localizedDescription]); } else { [audioRecorder prepareToRecord]; } 

我也试图使用AVAudioRecorder以AAC编码格式录制audio,最好在.m4a文件中录制。 我很快就能够将代码logging到核心audio文件(.caf)的AAC中,但是我无法使AVAudioRecorder正确格式化.m4a。 我正要使用较低级别的Audio Units API重写我的应用程序中的录音代码,但是我把它放在了后面的代码中,并添加了代码来处理共享audio会话。 一旦build立起来,我就回去尝试保存为.m4a,而且它工作正常。

以下是一些示例代码:

 NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docsDir = [dirPaths objectAtIndex:0]; NSURL *tmpFileUrl = [NSURL fileURLWithPath:[docsDir stringByAppendingPathComponent:@"tmp.m4a"]]; NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey, [NSNumber numberWithFloat:16000.0], AVSampleRateKey, [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, nil]; NSError *error = nil; AVAudioRecorder *recorder = [[AVAudioRecorder alloc] initWithURL:tmpFileUrl settings:recordSettings error:&error]; [recorder prepareToRecord]; AVAudioSession *session = [AVAudioSession sharedInstance]; [session setCategory:AVAudioSessionCategoryRecord error:nil]; [session setActive:YES error:nil]; [recorder record]; 

然后结束我使用的录音:

 [recorder stop]; AVAudioSession *session = [AVAudioSession sharedInstance]; int flags = AVAudioSessionSetActiveFlags_NotifyOthersOnDeactivation; [session setActive:NO withFlags:flags error:nil]; 

然后,“tmpFileUrl”中的文件可以随意使用。

这里是我的设置高质量和小文件大小(余额):

 NSDictionary *recordSettings = @{AVEncoderAudioQualityKey: @(AVAudioQualityMedium), AVFormatIDKey: @(kAudioFormatMPEG4AAC), AVEncoderBitRateKey: @(128000), AVNumberOfChannelsKey: @(1), AVSampleRateKey: @(44100)}; 

这给你一个接近CD质量的单声道AAC轨道。 你应该附加.m4a文件到处可读。 所有其他参数设置为设备默认值,这是大多数情况下您想要实现的。

 NSMutableDictionary *settings = [[NSMutableDictionary alloc] initWithCapacity:0]; [settings setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey]; [settings setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey]; [settings setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey]; [settings setValue :[NSNumber numberWithInt:8] forKey:AVLinearPCMBitDepthKey]; [settings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey]; [settings setValue :[NSNumber numberWithBool:NO] forKey:AVLinearPCMIsFloatKey]; //Encoder [settings setValue :[NSNumber numberWithInt:12000] forKey:AVEncoderBitRateKey]; [settings setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitDepthHintKey]; [settings setValue :[NSNumber numberWithInt:8] forKey:AVEncoderBitRatePerChannelKey]; [settings setValue :AVAudioQualityMin forKey:AVEncoderAudioQualityKey];