2011-08-21 73 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