Tuesday, April 12, 2016

Find out if earphones are plugged in or not

There may be usecase where you need to check whether head phones are plugged in or not before delving into other functionality.

Here is a way to achieve that.

1.Import AVFoundation framework.

2.Include AVFoundation header file in the place where you need to check whether head phone is plugged in or not.

3.Use the below method

-(BOOL)isHeadPhonePlugged
{
    AVAudioSessionRouteDescription *route=[[AVAudioSession sharedInstance]currentRoute];
    for (AVAudioSessionPortDescription *port in [route outputs]) {

        if ([[port portType]isEqualToString:AVAudioSessionPortHeadphones]) {
            return YES;
        }
        
    }
    return NO;


}

No comments:

Post a Comment