iPhone SDK AVAudioRecorder延迟录制之前

问题描述

| 我正在使用AVAudioRecorder从麦克风获取音频。我面临的问题是:第一次开始记录时,在我告诉记录器启动与实际启动之间有2秒的延迟。随后的录音呼叫执行得更快。这里有明显的错误吗?我可以采取什么措施来加快开始初始录制所需的时间。 我在viewDidLoad中初始化了记录器:
- (void)viewDidLoad
{
    NSString *fileName = @\"test.aiff\";
    [super viewDidLoad];

    NSString *docsDir = [NSSearchPathForDirectoriesInDomains(
                    NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];

    NSString *soundFile = [docsDir
                               stringByAppendingPathComponent:fileName];

    NSURL *soundFileURL = [NSURL fileURLWithPath:soundFile];

    NSDictionary *recordSettings = [NSDictionary 
                                    dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:AVAudioQualityMin],AVEncoderAudioQualityKey,[NSNumber numberWithInt:16],AVEncoderBitRateKey,[NSNumber numberWithInt: 2],AVNumberOfChannelsKey,[NSNumber numberWithFloat:44100.0],AVSampleRateKey,nil];

    NSError *error = nil;

    audioRecorder = [[AVAudioRecorder alloc]
                     initWithURL:soundFileURL
                     settings:recordSettings
                     error:&error];
}
然后,当我准备好时,我通过调用startRecoring方法来启动记录器:
-(void) startRecording{
    NSLog(@\"Trying to start Recording\");
    [audioRecorder record];
    NSLog(@\"Recording started\");
} 
这是Log输出,您可以看到两次NSLog调用之间大约有2.5秒。第一次单击但其他时间未单击:
First recorder call    
2011-04-13 15:41:47.495 AudioRecorderTest[6570:207] Trying to start Recording
2011-04-13 15:41:49.869 AudioRecorderTest[6570:207] Recording started

Next Recorder Call
2011-04-13 15:42:49.236 AudioRecorderTest[6570:207] Trying to start Recording
2011-04-13 15:42:49.246 AudioRecorderTest[6570:207] Recording started
    

解决方法

  在该位置创建一个音频文件   由url参数在   initWithURL:settings:error:方法。如果   该文件已经存在   位置,此方法将覆盖它。      此方法调用的准备   当您自动发生   通话记录。在以下情况下使用prepareToRecord   您想尽快开始录音   尽可能在通话记录中。 初始化录音机后尝试调用prepareToRecord     ,初始化记录器后,除了prepareToRecord之外,请尝试插入:
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *err = nil;

[audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
if(err) {
    NSLog(@\"audioSession: %@ %d %@\",[err domain],[err code],[[err userInfo] description]);
    return;
}
[audioSession setActive:YES error:&err];
if(err){
    NSLog(@\"audioSession: %@ %d %@\",[[err userInfo] description]);
    return;
}
UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory,sizeof(sessionCategory),&sessionCategory); 
    

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...