2009-11-11 267 views
3

我一直在关注Apple的文档,使用AVAudioSession类在iPhone上录制音频。我可以设置多个属性而不会出错(setActive,setCategory,setPreferredHardwareSampleRate),但我无法获得Apple的示例代码,以便为setPreferredIOBufferDuration工作。在iPhone上录制音频: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

看来这是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); 
+0

除其他事项外,Objective-C的方法有一个NSTimeInterval,这是一个双,而C方法采取Float32,这是一个浮点数。可能是Obj-C方法返回的“大小错误”错误的原因。 – lensovet 2010-03-27 20:05:11

+0

非常感谢,为我解决了这个问题! – DarkDust 2010-09-24 08:48:34