I am recording audio using AVAudioRecorder and the file is too big. Like 20 seconds, comes out to be 1.2mb? How c开发者_高级运维an I make it smaller? Thanks.
This is the best way I found, you can use it too:
NSMutableDictionary *settings = [[NSMutableDictionary alloc] initWithCapacity:0];
[settings setValue :[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];//格式
[settings setValue:[NSNumber numberWithFloat:8000.0] forKey:AVSampleRateKey]; //采样8000次
[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];//编码质量
Try using a different format and/or quality setting. Something like this should be a pretty small file:
NSDictionary *recordSettings =
[[NSDictionary alloc] initWithObjectsAndKeys:
[NSNumber numberWithFloat: 44100.0], AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatAppleIMA4], AVFormatIDKey,
[NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
[NSNumber numberWithInt: AVAudioQualityMedium],
AVEncoderAudioQualityKey,
nil];
AVAudioRecorder *newRecorder =
[[AVAudioRecorder alloc] initWithURL: myURL
settings: recordSettings
error: nil];
The size of the recorded audio file depends on the following factors. There are other factors too. But following would be worth noting.
1) Sample Rate
A 22050Hz sample rate will produce a much smaller file than a 44000Hz sample rate
2) Channels
Stereo output(2 channels) will produce twice the size of the file of that of a mono (1 channel)
3) Bit Rate
Bits used per sample.
You will need to come up with a setting that is best suitable for you so that it doesn't compromise on the quality as well as the size.
精彩评论