如何设置设置以获得以kb为单位的录制音频文件大小?

时间:2018-04-27 11:55:06

标签: ios objective-c

我从事录音和上传工作。在上传10secs音频时,我获得了4GB数据,我浏览并关注了StackOverflow中的一个答案,更改了如下所示的设置和音频文件格式为.3gp,但数据大小没有减少。

-(void) startRecording{
[_recordButton setTitle:@"Stop" forState:UIControlStateNormal];
NSError *error;
// Recording settings
NSLog(@"%f", [[AVAudioSession sharedInstance] sampleRate]);
NSMutableDictionary *settings = [NSMutableDictionary dictionary];
[settings setValue: [NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
[settings setValue: [NSNumber numberWithFloat:2000.0] forKey:AVSampleRateKey];
[settings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
[settings setValue:  [NSNumber numberWithInt: AVAudioQualityMax] forKey:AVEncoderAudioQualityKey];
[settings setValue:[NSNumber numberWithFloat:12000.0] forKey:AVEncoderBitRateKey];

NSArray *searchPaths =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath_ = [searchPaths objectAtIndex: 0];
NSString *pathToSave = [documentPath_ stringByAppendingPathComponent:@"AudioName.3gp"];
// File URL
NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH];
//Save recording path to preferences
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setURL:url forKey:@"Test1"];
[prefs synchronize];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

// Create recorder
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
recorder.delegate=self;
[recorder recordForDuration:10];
[self startTimerToMoveSlider];
}

有人可以指导我吗

1 个答案:

答案 0 :(得分:0)

最后解决了音频文件大小问题,这是我的代码,用于录制30秒音频并获得近60kb-70kb的文件大小。

-(void) startRecording{
[_recordButton setTitle:@"Stop" forState:UIControlStateNormal];
NSError *error;

NSString *pathToSave = [NSString stringWithFormat:@"%@/MySound.m4a", DOCUMENTS_FOLDER];

// File URL
NSURL *url = [NSURL fileURLWithPath:pathToSave];//FILEPATH];

// Recording settings
NSDictionary *settings = [NSDictionary dictionaryWithObjectsAndKeys:
                                [NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                [NSNumber numberWithFloat:8000.0], AVSampleRateKey,
                                [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,[NSNumber numberWithInt:12000],AVEncoderBitRateKey,
                                nil];
NSData *audioData = [NSData dataWithContentsOfFile:[url path] options: 0 error:&error];

//Save recording path to preferences
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setURL:url forKey:@"Test1"];
[prefs synchronize];

// Create recorder
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
recorder.delegate=self;
[recorder prepareToRecord];

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
[[AVAudioSession sharedInstance] setActive:YES error:nil];

[recorder recordForDuration:30];//recording for 30secs
[self startTimerToMoveSlider];//to move slider while recording

}

注意:#define DOCUMENTS_FOLDER [NSHomeDirectory()stringByAppendingPathComponent:@" Documents"]