Are Headphones Plugged In? iOS7

Are headphones plugged in? iOS7

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;
}

Swift -How to check if Headphones are Plugged In when the VC First Appears

You can use currentRoute.outputs on the AVAudioSession to check what outputs initially exist:

/// All current connected output device port types.
var outputPorts: [AVAudioSession.Port] { AVAudioSession.sharedInstance().currentRoute.outputs.map { $0.portType } }

let areHeadphonesConnected: Bool = outputPorts.contains(.headphones)

check whether headphones are plugged in when app starts

This should achieve what you want (iOS 6+ compatible)

- (BOOL)areHeadphonesPluggedIn {
NSArray *availableOutputs = [[AVAudioSession sharedInstance] currentRoute].outputs;
for (AVAudioSessionPortDescription *portDescription in availableOutputs) {
if ([portDescription.portType isEqualToString:AVAudioSessionPortHeadphones]) {
return YES;
}
}
return NO;
}

Detecting if headphones are plugged into iPhone

http://developer.apple.com/iphone/library/samplecode/SpeakHere/Introduction/Intro.html

In this project there is a code-snippet where it pauses recording if the headphones is unpluged. Maybe you can use it to achieve your result.

Good luck!

(edit)

You will have to study the SpeakHereController.mm file.

I found this code in the awakeFromNib method

// we do not want to allow recording if input is not available
error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error);
btn_record.enabled = (inputAvailable) ? YES : NO;

// we also need to listen to see if input availability changes
error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self);
if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);

Detect plugged & unplugged event on headphone jack in Xamarin.iOS (iOS 7)

    using System.Runtime.InteropServices;
using MonoTouch.AudioToolbox;

public override void ViewDidLoad()
{
base.ViewDidLoad();
.
.
.
AudioSession.Initialize(null, NSRunLoop.NSDefaultRunLoopMode);
AudioSession.AudioRouteChanged += AudioSession_AudioRouteChanged;
}

void AudioSession_AudioRouteChanged (object sender, AudioSessionRouteChangeEventArgs e)
{
if (e.CurrentOutputRoutes [0] == AudioSessionOutputRouteKind.Headphones)
{
//Code when is plugged
}
else
{
//Code when is unplugged
}
}

How to detect if a bluetooth headset plugged or not IOS 8?

I was able to detect whether a bluetooth headset (HFP) device was currently connected using the following:

NSArray *arrayInputs = [[AVAudioSession sharedInstance] availableInputs];
for (AVAudioSessionPortDescription *port in arrayInputs)
{
if ([port.portType isEqualToString:AVAudioSessionPortBluetoothHFP])
{
bHas = YES;
break;
}
}

However, your AVAudioSession category must be set as AVAudioSessionCategoryPlayAndRecord in order for this to work. If it isn't, the port will not show up in the list even if the HFP device is connected.

Is there an event for when the headphones are unplugged?

See Responding to Route Changes from the Audio Session Programming Guide.



Related Topics



Leave a reply



Submit