I am facing this problem:
I record some audio using the iPhone microphone and upload it to my server. I record
for say, 40 seconds and call the upload
function after 41 seconds (timer). Now, the recorded file that is uploaded to the server is not the full file since the function
audioRecorderDidFinishRecording: successfully:
is not called before I call the upload
function. Its called only after the upload
function is called - no matter when I call the upload
function - 10 seconds later or 100 seconds later.
In the upload function, I use ASIHTTPRequest and ASIFormDataRequest to upload the file.
Can anyone kindly tell me why this is happening ? Thanks.
Edit #1:
If the upload
method is not called, the audioRecorderDidFinishRecording: successfully:
method is never invoked. Pretty strange !
Edit #2:
Relevant methods:
(void) upload
{
NSString *urlString = @"<SOME_LINK>";
NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
NSData *fileData=[NSData dataWithContentsOfURL:recordedTmpFile];
[request setData:fileData withFileName:[fileName copy] andContentType:@"audio/x-caf" forKey:@"userfile"];
[request setPostValue:[appDelegate getRandomXML] forKey:@"random"];
[request setRequestMethod:@"POST"];
[request setDelegate:self];
[request setShouldContinueWhenAppEntersBackground:YES];
[request startSynchronous];
}
- (void) record
{
NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleLossless] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];
NSDateFormatter *formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"hh:mm:ss"];
NSString *dateString=[formatter stringFromDate:[NSDate date]];
fileName=[@"recordTest" stringByAppendingFormat:@"%@.alac", dateString];
[formatter release];
NSString *documentDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *mediaPath=[documentDirectory stringByAppendingPathComponent:[fileName copy]];
NSString *mediaPathFinal=[NSString stringWithFormat:@"file://localhost%@",mediaPath];
recordedTmpFile = [NSURL URLWithString:[mediaPathFinal stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
recorder.delegate=self;
[recorder recordForDuration:(NSTimeInterval) ([[appDelegate getTimeToRecord] intValue]+1)];
}
- (void) audioRecorderDidFinishRecording:(AVAudioRecorder *) recorder successfully:(BOOL)flag
{
NSLog(@"DONE");
}
- (void) audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *) recorder error:(NSError *) error
{
NSLog(@"ERROR");
}
- (void) viewWillAppear:(BOOL)animated
{
AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];
[audioSession setDelegate:self];
[self record];
[NSTimer scheduledTimerWithTimeInterval:([[appDelegate getTimeToRecord] intValue]+1) target:self selector:@selector(upload) userInfo:nil repeats:NO];
}
You should call the -upload
method inside audioRecorderDidFinishRecording:successfully:
rather than in viewWillAppear:
.