Thursday, June 6, 2013

AVAudioSession setPreferredIOBufferDuration error example in Objective C (iOS).


AVAudioSession setPreferredIOBufferDuration error

Sets the preferred audio I/O buffer duration, in seconds.

- (BOOL)setPreferredIOBufferDuration:(NSTimeInterval)duration error:(NSError **)outError

Parameters of [AVAudioSession setPreferredIOBufferDuration error]
duration
The audio I/O buffer duration, in seconds, that you want to use. The available range for I/O buffer duration is 0.005 through 0.093 s, corresponding to a range of 256 through 4,096 sample frames at 44.1 kHz.
outError
On input, specify a pointer to an error object. If an error occurs, the pointer is set to an NSError object that describes the error. If you do not want error information, pass in nil.

Return Value of [AVAudioSession setPreferredIOBufferDuration error]
Returns YES on success or NO on failure.

Discussion of [AVAudioSession setPreferredIOBufferDuration error]
The audio I/O buffer duration is the number of seconds for a single audio input/output cycle. For example, with an I/O buffer duration of 0.005 s, on each audio I/O cycle:

You receive 0.005 s of audio if obtaining input.
You must provide 0.005 s of audio if providing output.
To obtain the actual I/O buffer duration, call the AudioSessionGetProperty function using the kAudioSessionProperty_CurrentHardwareIOBufferDuration key as its inID parameter.

For more information see “Querying and Using Audio Hardware Characteristics” in Audio Session Programming Guide.

AVAudioSession setPreferredIOBufferDuration error example.
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
audioSession.delegate = self;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];    
[[AVAudioSession sharedInstance] setPreferredHardwareSampleRate:44100 error:nil];
[[AVAudioSession sharedInstance] setPreferredIOBufferDuration:30 error:nil];

UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
Float32 hardvol = 1.0;
AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);
AudioSessionSetProperty(kAudioSessionProperty_CurrentHardwareOutputVolume, sizeof(hardvol), &hardvol);
UInt32 doSetProperty = 1;
AudioSessionSetProperty(kAudioSessionProperty_OverrideCategoryMixWithOthers, sizeof(doSetProperty), &doSetProperty);
[[AVAudioSession sharedInstance] setActive:YES error: nil];

Example of [AVAudioSession setPreferredIOBufferDuration error].
- (void)setupAudio {
    [[AVAudioSession sharedInstance] setDelegate:self];
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:nil];
    NSError *activationError = nil;
    [[AVAudioSession sharedInstance] setActive: YES error:&activationError];

    NSLog(@"setupAudio ACTIVATION ERROR IS %@", activationError);
    [[AVAudioSession sharedInstance] setPreferredIOBufferDuration:0.1 error:&activationError];
    NSLog(@"setupAudio BUFFER DURATION ERROR IS %@", activationError);
}

End of AVAudioSession setPreferredIOBufferDuration error example article.