录制前iPhone SDK AVAudioRecorder延迟

时间:2011-04-13 19:48:02

标签: iphone ios audio delay 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");
} 

这是日志输出,您可以看到两个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

2 个答案:

答案 0 :(得分:4)

  

在该位置创建音频文件   由url参数指定   initWithURL:settings:error:方法。如果   那个文件已经存在了   location,此方法会覆盖它。

     

此方法调用的准备工作   你自动发生   通话记录。使用prepareToRecord时   你希望录音能够快速开始   在打电话给记录时尽可能。

初始化录音机后尝试拨打prepareToRecord

答案 1 :(得分:0)

初始化录像机后,除了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 domain], [err code], [[err userInfo] description]);
    return;
}
UInt32 sessionCategory = kAudioSessionCategory_PlayAndRecord;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);