2011-08-21 72 views
2

我面临这个问题:问题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]; 


} 
+0

为什么在完成记录回调方法被调用之后上传文件而不是尝试使用定时器呢? –

+0

@Jason:除非调用'upload'函数,否则完成录制回调方法永远不会被调用...:-s – ahsan

+0

您可以发布整个上传方法吗?只需替换可能是私密的任何内容,如上传网址或密码,但否则包括整个上传实施? –

回答

1

你应该调用-upload方法内audioRecorderDidFinishRecording:successfully:而不是viewWillAppear:

+0

:如果未调用upload方法,那么audioRecorderDidFinishRecording:successfully:从不会被调用...... – ahsan

+0

这很奇怪 - 我我确定问题在别的地方。顺便提一下, 1)为什么你在'record'方法里面调用*' - [AVAudioRecorder记录]和' - [AVAudioRecorder recordForDuration:]'? 2)前面提到的这两种方法的文档都声称'prepareToRecord'是隐式调用的。所以你不需要在'record'方法中明确地调用它。 3)次要挑剔,但不需要在'record'方法中将'self'设置为委托两次。 –

+0

对不起..有点忽略了这些东西.... – ahsan