Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are headphones plugged in? iOS7

Developing an app for an iPhone with audio files that need to be listened too through headphones.

How do I check if headphones aren't plugged in so I can tell the user to plug in headphones.

I have the following code from another thread but the audioSessionGetProperty method is deprecated. Anyone know how to alter the following code to make this work OR have there own code/solution.

Thanks.

- (BOOL)isHeadsetPluggedIn {     UInt32 routeSize = sizeof (CFStringRef);     CFStringRef route;       //Maybe changing it to something like the following would work for iOS7?     //AVAudioSession* session = [AVAudioSession sharedInstance];     //OSStatus error = [session setCategory:kAudioSessionProperty_AudioRoute...?       //the line below is whats giving me the warning     OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,                                               &routeSize,                                               &route);      /* Known values of route:      * "Headset"      * "Headphone"      * "Speaker"      * "SpeakerAndMicrophone"      * "HeadphonesAndMicrophone"      * "HeadsetInOut"      * "ReceiverAndMicrophone"      * "Lineout"      */      if (!error && (route != NULL)) {          NSString* routeStr = (__bridge NSString*)route;          NSRange headphoneRange = [routeStr rangeOfString : @"Head"];          if (headphoneRange.location != NSNotFound) return YES;      }      return NO; } 
like image 565
4GetFullOf Avatar asked Jan 22 '14 19:01

4GetFullOf


People also ask

Does iPhone 7 have plug in headphones?

The iPhone 7 doesn't have a built-in headphone jack, but you can use the included headphones that plug into the phone's Lightning port. Use AirPods or other wireless headphones. Pair the headphones, then set the audio to play to them via the Control Center.

Why does it say headphones are plugged in?

Clean the headphone jack When you connect the headphones, the plug pushes the lint further to the bottom of the jack. If the lint is conductive, it may be messing with the electronic circuits inside the jack, causing the phone to register it as a pair of headphones plugged in.

Where do you plug headphones into iPhone?

Lightning to 3.5mm Headphone Jack Adapter Your iOS device also needs iOS 10 or later. Plug your Lightning to 3.5mm Headphone Jack Adapter into the Lightning connector on your iOS device and plug the other end into your headphones.

Does iPhone 7 plus have headphone jack?

Apple has done it again with the iPhone 7 Plus, a phone that's as powerful as it is controversial. In camera tests it wows with 2× optical zoom and sharp photos, and it's finally water resistant; but there's just one little problem: it doesn't have a headphone jack.


1 Answers

This should work, but I cannot test it right now, I'll do in the evening.

- (BOOL)isHeadsetPluggedIn {     AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];     for (AVAudioSessionPortDescription* desc in [route outputs]) {         if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])             return YES;     }     return NO; } 
like image 109
Antonio E. Avatar answered Oct 19 '22 21:10

Antonio E.