views:

268

answers:

2

Hello,

I have been following Apple's documentation to record audio on the iPhone using the AVAudioSession class. I can set several properties without error ( setActive, setCategory, setPreferredHardwareSampleRate ) but I cannot get Apple's sample code to work for setPreferredIOBufferDuration.

Here's my code:

- (void) initX {

NSError *setPreferenceError = nil; NSTimeInterval preferredBufferDuration = 0.005;

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

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

It produces the following output:

Error Domain=NSOSStatusErrorDomain Code=561211770 "Operation could not be completed. (OSStatus error 561211770.)"

I am calling this method from the main Application Delegate, as part of the applicationDidFinishLaunching method. All I am doing is initializing things at this stage. I have imported AVFoundation/AVFoundation.h after adding AVFoundation.framework to the project.

Any suggestions would be appreciated.

A: 

IT seems its a property size issue, I have the same problem and didnt solve it yet :) kAudioHardwareBadPropertySizeError = 561211770

lll
+1  A: 

It appears that this is a bug in Apple's code; use the pure C interface instead:

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

Then to check for errors use

if (propertySetError) NSLog(@"Failed to set shorter I/O buffer on AVAudioSession, code %d", propertySetError);
lensovet
among other things, the Objective-C method takes an NSTimeInterval, which is a double, whereas the C method take a Float32, which is a float. Probably the reason for the "wrong size" error that the Obj-C method returns.
lensovet
@lensovet: Thanks a lot, that fixed the problem for me !
DarkDust