audioRecorderDidFinishRecording 的问题:成功:
我面临这个问题:
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该在
audioRecorderDidFinishRecording:successively:
中调用-upload
方法,而不是在viewWillAppear:
中。You should call the
-upload
method insideaudioRecorderDidFinishRecording:successfully:
rather than inviewWillAppear:
.