2009-11-11 270 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