Thursday, June 6, 2013

AVAudioSession inputGainSettable example in Objective C (iOS).

AVAudioSession inputGainSettable

A Boolean value indicating whether the input gain can be set. (read-only)

@property(readonly, getter=isInputGainSettable) BOOL inputGainSettable

Discussion of [AVAudioSession inputGainSettable]
Some inputs may not provide the ability to set the input gain, so it is important to check this property before attempting to set the input gain.

AVAudioSession inputGainSettable example.
On ios6+ you can use AVAudioSession properties

        CGFloat gain = sender.value;
        NSError* error;
        self.audioSession = [AVAudioSession sharedInstance];
        if (self.audioSession.isInputGainSettable) {
            BOOL success = [self.audioSession setInputGain:gain
               if (!success){} //error handling
        } else {
            NSLog(@"ios6 - cannot set input gain");
On ios5 you can get/set audio input gain properties using AudioSession functions

    UInt32 ui32propSize = sizeof(UInt32);
    UInt32 f32propSize = sizeof(Float32);
    UInt32 inputGainAvailable = 0;
    Float32 inputGain = sender.value;

    OSStatus err =
                            , &ui32propSize
                            , &inputGainAvailable);

    if (inputGainAvailable) {
    OSStatus err =
                             , sizeof(inputGain)
                             , &inputGain);
    } else {
        NSLog(@"ios5 - cannot set input gain");
    OSStatus err =
                              , &f32propSize
                              , &inputGain);
    NSLog(@"inputGain: %0.2f",inputGain);

Example of [AVAudioSession inputGainSettable].
self.audioSession = [AVAudioSession sharedInstance];
if (self.audioSession.isInputGainSettable) {
    BOOL success = [self.audioSession setInputGain:sender.value error:&error];
    if (!success) NSLog(@"inputGain error: %@",error);

End of AVAudioSession inputGainSettable example article.