2011-10-19 80 views
3

我正在使用AVAudioRecorder录制音频,但我正在经历按钮按下和开始录制之间的4秒延迟。AVAudioRecorder记录4秒滞后

这是我的设置代码:

NSDictionary *recordSettings = [NSDictionary dictionaryWithObjectsAndKeys:

        [NSNumber numberWithFloat: 16000.0],AVSampleRateKey, 
            [NSNumber numberWithInt: kAudioFormatAppleIMA4],AVFormatIDKey, 
            [NSNumber numberWithInt: 1], AVNumberOfChannelsKey, 
            [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil]; 

    NSError *error = nil; 

    audioRecorder = [[AVAudioRecorder alloc] 
        initWithURL:soundFileURL 
        settings:recordSettings 
        error:&error]; 
    if (error) 
    { 
     NSLog(@"error: %@", [error localizedDescription]); 

    } else { 
     NSLog(@"prepare to record"); 
     [audioRecorder prepareToRecord]; 
    } 

-(void)record {

NSLog(@"Record"); 

//[audioRecorder prepareToRecord]; 

if (!audioRecorder.recording) 

{ 

    NSLog(@"Record 2"); 

    [audioRecorder record]; 

    NSLog(@"Record 3");   

} 

}

记录被称为按下按钮的功能。我知道prepareToRecord是通过'record'隐式调用的,但我想看看它是否会影响延迟。它不是。

这里的控制台日志:

2011-10-18 21:48:06.508 [2949:707] Record 
2011-10-18 21:48:06.509 [2949:707] Record 2 
2011-10-18 21:48:10.047 [2949:707] Record 3 

有大约3.5秒钟,这两点开始录制之前。

这些设置对于iPhone来说太多了吗? (iPhone 4)。我初始化它错了吗?

回答

1

我在iOS5/iPhone4组合上看到了同样的问题。 iOS4和iPhone4S都很好。在旧硬件上使用新操作系统似乎存在问题。我也尝试过很多设置组合。

尝试将AVAudioSession类别,当你初始化录音机:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];

0

我对iPod的同样的问题touch第4代(iOS4的,的iOS5和越狱 - 我用更多的设备),并在不同设备上它运行不同,但我认为@Charles Chase是正确的,它取决于您正在测试的设备的iOS。你recordSettings字典是好的,代码也期待权,除了一件事,你需要补充一点:

audioRecorder.delegate = self; 

你的alloc后立即和init刻录机:

audioRecorder = [[AVAudioRecorder alloc] 
          initWithURL:soundFileURL 
          settings:recordSettings 
          error:&error]; 

    audioRecorder.delegate = self; 
0

我有同样的问题。另外,由于我在“回放”和“录制”模式之间切换,这会不断造成长时间的延迟。但是当我切换到播放并录制时,它拒绝在iPhone上使用扬声器,它只会使用耳机扬声器。

解决方案:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; 
UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker; 
AudioSessionSetProperty(kAudioSessionProperty_OverrideAudioRoute, 
         sizeof(audioRouteOverride), &audioRouteOverride);