iphone – iPad上的AVAudioRecorder米延迟

前端之家收集整理的这篇文章主要介绍了iphone – iPad上的AVAudioRecorder米延迟前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我无法从AVAudioRecorder获得准确的抄表(在iPad上进行测试).

在音量上升时似乎工作正常,但是当音量下降时会发生延迟.例如:我对着麦克风说话,然后慢慢提高声音.正如我所希望的那样,读数从-35.567增加到-34.678到-10.579,但是当我停止说话时,它会延迟1到2秒,然后再下降到-35.567(或者无论发生什么).
NSLog继续从循环更新,但即使声音已经结束,仪表编号在延迟期间保持不变.

我已经添加了下面代码的要点,如果需要,我很乐意提供完整的代码.

我像这样初始化录音机:

AVAudioSession * audioSession = [AVAudioSession sharedInstance];
[audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error];
[audioSession setActive:YES error: &error];

NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
[recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
[recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
[recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey];

recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory()  stringByAppendingPathComponent: [NSString stringWithString: @"Recording.caf"]]];

recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];
[recorder setDelegate:self];
[recorder prepareToRecord];
[recorder setMeteringEnabled:YES];

[recorder record];

并循环更新仪表:

-(void) loop:(ccTime)dt 
{
    if(isRecording == YES)
    {        
        //get volume levels
        [recorder updateMeters];
        float level = [recorder peakPowerForChannel:0];
        NSLog(@"Vol: %f",level);
    }
}

编辑:我还应该提到我正在使用循环的Cocos2d计划:

[self schedule:@selector(loop:)];

有什么想法会有这么长的延迟?

编辑:我已尝试使用平均峰值功率,这没有延迟.所以我可以用它作为一种解决方法.但是我宁愿不使用和平均峰值功率,也很好理解发生了什么.

解决方法

我敢肯定,大多数人已经想到这一点,但如果你想减少你的计量延迟,你需要使用AudioQueue或RemoteIO.在这里看到更好的解释:

Confusion with meters in AVAudioRecorder

猜你在找的Xcode相关文章