Via AVAudioRecorder I do a 16 second audio recording then upload it on a server via AFNetworking 2.0
On iOS 8 everything is fine. But when running on iOS7 the server returns file is too large(code:413).
-(void)viewDidLoad{
file_url_str = @"audioFile.m4a";
audio_duration = 15.0;
[self initAudioRecorder];
}
-(void)initAudioRecorder{
// Setup audio session
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
NSURL * audio_file_URL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:file_url_str]];
NSDictionary * audio_Settings = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithFloat:44100],AVSampleRateKey,
[NSNumber numberWithInt: kAudioFormatMPEG4AAC],AVFormatIDKey,
[NSNumber numberWithInt: 1],AVNumberOfChannelsKey,
[NSNumber numberWithInt:AVAudioQualityMedium],AVEncoderAudioQualityKey,nil];
audio_recorder = [[AVAudioRecorder alloc]initWithURL:audio_file_URL
settings:audio_Settings
error:nil];
audio_recorder.delegate = self;
[audio_recorder prepareToRecord];
}
-(void)recordBtnPressed:(id)sender{
//[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryRecord error:nil];
record_btn.selected = !record_btn.selected;
if(record_btn.selected){
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[audio_recorder recordForDuration:16.0];
}
}
-(void)stopRecording{
[audio_recorder stop];
}
#pragma mark - AVAudio Delegate Selectors
#pragma mark -
-(void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag{
NSLog(@"done record");
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
[audioSession setActive:NO error:nil];
[self initAudioPlayer];
[self setData];
}
-(void)initAudioPlayer{
NSURL * audio_file_URL = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingString:file_url_str]];
audio_player = [[AVAudioPlayer alloc] initWithContentsOfURL:audio_file_URL error:nil];
audio_player.delegate = self;
}
-(void)setData{
NSString * audio_file = [NSTemporaryDirectory() stringByAppendingPathComponent:file_url_str];
//This is the data sent to the server
audio_data = [[NSData alloc]initWithContentsOfFile:audio_file];
}
AFNETWORKING 2.0
- this is under a subclass file of AFHTTPSessionManager
//This is how I set the serializer
//"self" pertains to AFHTTPSessionManager
self.responseSerializer = [HAJSONResponseSerializer serializer];
self.requestSerializer = [AFJSONRequestSerializer serializer];
#pragma mark - Audio Upload
#pragma mark -
-(void)uploadAudio:(NSData *)audioFile{
[self setTokenHeader]; //method that sets token header
[self POST:@"/users/song/record" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:audioFile name:@"profile_song" fileName:@"profilesong.m4a" mimeType:@"audio/mp4a-latm"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
//if (success) success(responseObject);
if([self.delegate respondsToSelector:@selector(HAHTTPClient:audioUploadRequestSuccessful:)]){
[self.delegate HAHTTPClient:self audioUploadRequestSuccessful:responseObject];
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
if([self.delegate respondsToSelector:@selector(HAHTTPClient:audioUploadDidFailWithError:)]){
[self.delegate HAHTTPClient:self audioUploadDidFailWithError:error];
}
}];
}