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; }
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.
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.
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.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With