我面臨這個問題:問題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
您可以發佈整個上傳方法嗎?只需替換可能是私密的任何內容,如上傳網址或密碼,但否則包括整個上傳實施? –