简体   繁体   中英

How to set settings to get recorded audio file size in kb?

I'm working on audio recording and uploading. While uploading 10secs audio I'm getting the 4GB data, I browsed and followed one of the answers in StackOverflow, changed settings as shown below and audio file format to .3gp, but data size not reduced.

-(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];
}

Can anybody please guide me

Finally solved audio file size issue, here is my code for recording 30seconds audio and getting file size nearly 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

}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM