简体   繁体   中英

Recording audio on iPhone: error with setPreferredIOBufferDuration

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM