我面临这个问题:
我使用 iPhone 麦克风录制一些音频并将其上传到我的服务器。我record
说 40 秒并upload
在 41 秒后调用该函数(计时器)。现在,上传到服务器的录制文件不是完整文件,因为该功能
audioRecorderDidFinishRecording: successfully:
在我调用upload
函数之前不调用。它仅在upload
调用函数后调用 - 无论我何时调用upload
函数 - 10 秒后或 100 秒后。
在上传功能中,我使用 ASIHTTPRequest 和 ASIFormDataRequest 来上传文件。
谁能告诉我为什么会这样?谢谢。
编辑#1:如果 upload
未调用该audioRecorderDidFinishRecording: successfully:
方法,则永远不会调用该方法。很奇怪!
编辑#2:
相关方法:
(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];
}