我面临这个问题:问题audioRecorderDidFinishRecording:成功:
我使用iPhone的麦克风记录一些声音并把它上传到我的服务器。我说的是record
,40秒并且在41秒(定时器)之后调用upload
函数。现在,被上传到服务器的记录文件是不是因为功能
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];
}
为什么在完成记录回调方法被调用之后上传文件而不是尝试使用定时器呢? –
@Jason:除非调用'upload'函数,否则完成录制回调方法永远不会被调用...:-s – ahsan
您可以发布整个上传方法吗?只需替换可能是私密的任何内容,如上传网址或密码,但否则包括整个上传实施? –