首页 > 解决方案 > 如何设置设置以获取以 kb 为单位的录制音频文件大小?

问题描述

我正在处理录音和上传。在上传 10 秒的音频时,我得到了 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];
}

谁能指导我

标签: iosobjective-c

解决方案


终于解决了音频文件大小问题,这是我录制 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"]


推荐阅读