在iPhone上录制音频:setPreferredIOBufferDuration出错

时间:2009-11-11 11:10:05

标签: iphone audio recording

我一直在关注Apple的文档,使用AVAudioSession类在iPhone上录制音频。我可以设置多个属性而不会出现错误(setActivesetCategorysetPreferredHardwareSampleRate),但我无法让Apple的示例代码适用于setPreferredIOBufferDuration

这是我的代码:

- (void) initX {
 NSError *setPreferenceError = nil;
 NSTimeInterval preferredBufferDuration = 0.005;

 [[AVAudioSession sharedInstance]
  setPreferredIOBufferDuration: preferredBufferDuration
  error: &setPreferenceError];

 if (setPreferenceError != nil) {
  NSLog( @"%@", setPreferenceError );
 }
}

它产生以下输出:

  

错误域= NSOSStatusErrorDomain代码= 561211770“操作无法完成。(OSStatus错误561211770。)”

我从主Application Delegate调用此方法,作为applicationDidFinishLaunching方法的一部分。我正在做的就是在这个阶段初始化事物。在将AVFoundation.framework添加到项目后,我导入了AVFoundation / AVFoundation.h。

1 个答案:

答案 0 :(得分:1)

这似乎是Apple代码中的一个错误;改为使用纯C接口:

OSStatus propertySetError = 0;
Float32 preferredBufferDuration = 0.005;
propertySetError = AudioSessionSetProperty(kAudioSessionProperty_PreferredHardwareIOBufferDuration, sizeof(preferredBufferDuration), &preferredBufferDuration);

然后检查错误使用

if (propertySetError) NSLog(@"Failed to set shorter I/O buffer on AVAudioSession, code %d", propertySetError);
相关问题