audioRecorderDidFinishRecording 的问题:成功:

发布于 2024-11-30 12:30:50 字数 3173 浏览 0 评论 0原文

我面临这个问题:

我使用 iPhone 麦克风录制一些音频并将其上传到我的服务器。我记录例如40秒,并在41秒(计时器)后调用上传函数。现在,上传到服务器的录制文件不是完整文件,因为

audioRecorderDidFinishRecording:  successfully:  

在调用 upload 函数之前未调用该函数。它仅在调用 upload 函数之后调用 - 无论我何时调用 upload 函数 - 10 秒后或 100 秒后。

在上传功能中,我使用ASIHTTPRequest和ASIFormDataRequest来上传文件。

谁能告诉我为什么会发生这种情况?谢谢。

编辑#1: 如果未调用 upload 方法,则永远不会调用 audioRecorderDidFinishRecording: successed: 方法。很奇怪!

编辑#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];


}

I am facing this problem:

I record some audio using the iPhone microphone and upload it to my server. I record for say, 40 seconds and call the upload function after 41 seconds (timer). Now, the recorded file that is uploaded to the server is not the full file since the function

audioRecorderDidFinishRecording:  successfully:  

is not called before I call the upload function. Its called only after the upload function is called - no matter when I call the upload function - 10 seconds later or 100 seconds later.

In the upload function, I use ASIHTTPRequest and ASIFormDataRequest to upload the file.

Can anyone kindly tell me why this is happening ? Thanks.

Edit #1:
If the upload method is not called, the audioRecorderDidFinishRecording: successfully: method is never invoked. Pretty strange !

Edit #2:

Relevant methods:

(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];


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里兽 2024-12-07 12:30:50

您应该在 audioRecorderDidFinishRecording:successively: 中调用 -upload 方法,而不是在 viewWillAppear: 中。

You should call the -upload method inside audioRecorderDidFinishRecording:successfully: rather than in viewWillAppear:.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文