ios - 如何设置设置以获取以 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];
}
谁能指导我
解决方案
终于解决了音频文件大小问题,这是我录制 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"]
推荐阅读
- c++ - 为什么makefile强制覆盖目标
- ruby - Ruby:为子字符串测试 ruby 字符串失败(无法识别子字符串)
- excel - excel如何为数据透视表、数据透视RowFields等分配索引号?
- python - Python 中的 QDBusInterface Upower 属性为无
- sql - SQL 创建额外的列,从表的两列中获取数据
- html - 如何在 HTML 中存储地理位置?
- javascript - 如何在不同的文本区域显示用户的输入?
- angular - 从拖放组件中获取json数据
- sql - 我如何总结每个产品的每个月的数据?
- regex - 如何为正则表达式创建业务逻辑并将数据保存到 csv 文件