Thursday, June 6, 2013

AVAudioSession currentRoute example in Objective C (iOS).


AVAudioSession currentRoute

The current audio routing information. (read-only)

@property(readonly) AVAudioSessionRouteDescription *currentRoute

Discussion of [AVAudioSession currentRoute]
The current audio route contains information about the input and output ports involved in the session. You can use the object in this property to retrieve those ports and get information about them.

AVAudioSession currentRoute example.
- (BOOL)isHeadsetPluggedIn
{
    // Get array of current audio outputs (there should only be one)
    NSArray *outputs = [[AVAudioSession sharedInstance] currentRoute].outputs;

    NSString *portName = [[outputs objectAtIndex:0] portName];

    if ([portName isEqualToString:@"Headphones"]) {
        return YES;
    }

    return NO;
}

Example of [AVAudioSession currentRoute].
AVAudioSessionRouteDescription *route = [[AVAudioSession sharedInstance] currentRoute];

for(AVAudioSessionPortDescription *port in route.outputs) {
    NSLog(@"AUDIO_OUTPUT IS NOW: %@",port.portType);
}

End of AVAudioSession currentRoute example article.