Thursday, June 6, 2013

AVAudioSession inputAvailable example in Objective C (iOS).


AVAudioSession inputAvailable

A Boolean value indicating whether an audio input path is available. (read-only)

@property(readonly, getter=isInputAvailable) BOOL inputAvailable

Discussion of [AVAudioSession inputAvailable]
Use this property at launch time to determine whether the current device supports audio input. To respond to a change in the availability of audio input, register for the AVAudioSessionInputDidBecomeAvailableNotification and AVAudioSessionInputDidBecomeUnavailableNotification notifications.

This property contains the value YES if a path is available or NO if one is not.

AVAudioSession inputAvailable example.
AudioSessionInitialize(NULL, NULL, NULL, NULL);   
UInt32 propertySize, micConnected;
    AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);
    [self updateMicStatus:micConnected]; // user-created method

In IOS 6 inputIsAvailable is deprecated. In the future we need to use inputAvailable:

BOOL audioHWAvailable = audioSession.inputAvailable;

Example of [AVAudioSession inputAvailable].
//    audioRecorder.meteringEnabled = YES;
//
BOOL audioHWAvailable = audioSession.inputAvailable;
if (! audioHWAvailable) {
    UIAlertView *cantRecordAlert =
    [[UIAlertView alloc] initWithTitle: @"Warning"
                               message: @"Audio input hardware not available"
                              delegate: nil
                     cancelButtonTitle:@"OK"
                     otherButtonTitles:nil];
    [cantRecordAlert show];
    return;
}

AVAudioSession inputAvailable example.
CFStringRef newRoute;
 UInt32 size;
 size = sizeof(CFStringRef);
 OSStatus error = AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute);
 if (error)
{

}
else
{
CFShow(newRoute);
}

End of AVAudioSession inputAvailable example article.