Are headphones plugged in? iOS7 - ios

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

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

Just to extend #Antonio's answer. If you need to detect whether the user has pulled out or plugged in the headphone.
#import <AVFoundation/AVFoundation.h>
// [AVAudioSession sharedInstance]; // #Boris edited: you may need it if there is no `AVAudioSession instance` created before. If doesn't work, uncomment this line.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(audioRouteChangeListenerCallback:)
name:AVAudioSessionRouteChangeNotification
object:nil];
// don't forget to `removeObserver:`
// If the user pulls out he headphone jack, stop playing.
- (void)audioRouteChangeListenerCallback:(NSNotification*)notification
{
NSDictionary *interuptionDict = notification.userInfo;
NSInteger routeChangeReason = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
switch (routeChangeReason) {
case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
NSLog(#"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
NSLog(#"Headphone/Line plugged in");
break;
case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
NSLog(#"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
NSLog(#"Headphone/Line was pulled. Stopping player....");
break;
case AVAudioSessionRouteChangeReasonCategoryChange:
// called at start - also when other audio wants to play
NSLog(#"AVAudioSessionRouteChangeReasonCategoryChange");
break;
}
}

Swift 3:
To check if headphones are connected
extension AVAudioSession {
static var isHeadphonesConnected: Bool {
return sharedInstance().isHeadphonesConnected
}
var isHeadphonesConnected: Bool {
return !currentRoute.outputs.filter { $0.isHeadphones }.isEmpty
}
}
extension AVAudioSessionPortDescription {
var isHeadphones: Bool {
return portType == AVAudioSessionPortHeadphones
}
}
Then you can just print("isHeadphones connected: \(AVAudioSession.isHeadphonesConnected)")
Listening to Changes
In Swift 3 the syntax is this:
func handleRouteChange(_ notification: Notification) {
guard
let userInfo = notification.userInfo,
let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber,
let reason = AVAudioSessionRouteChangeReason(rawValue: reasonRaw.uintValue)
else { fatalError("Strange... could not get routeChange") }
switch reason {
case .oldDeviceUnavailable:
print("oldDeviceUnavailable")
case .newDeviceAvailable:
print("newDeviceAvailable")
if AVAudioSession.isHeadphonesConnected {
print("Just connected headphones")
}
case .routeConfigurationChange:
print("routeConfigurationChange")
case .categoryChange:
print("categoryChange")
default:
print("not handling reason")
}
}
func listenForNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)
}
Notice use of:
if AVAudioSession.isHeadphonesConnected {
print("Just connected headphones")
}

#Warif's code in Swift 2.0 with little changes ...
func audioRouteChangeListenerCallback (notif: NSNotification){
let userInfo:[NSObject:AnyObject] = notif.userInfo!
println("\(userInfo)")
let routChangeReason = UInt((userInfo[AVAudioSessionRouteChangeReasonKey]?.integerValue)!)
switch routChangeReason {
case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue:
self.println("Headphone/Line plugged in");
break;
case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue:
//If the headphones was pulled move to speaker
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker)
} catch _ {
}
self.println("Headphone/Line was pulled. Stopping player....");
break;
case AVAudioSessionRouteChangeReason.CategoryChange.rawValue:
// called at start - also when other audio wants to play
self.println("AVAudioSessionRouteChangeReasonCategoryChange");
break;
default:
break;
}
}
:D

In Swift (as of 1.2):
func headsetPluggedIn() -> Bool {
let route = AVAudioSession.sharedInstance().currentRoute
return (route.outputs as! [AVAudioSessionPortDescription]).filter({ $0.portType == AVAudioSessionPortHeadphones }).count > 0
}

Swift 3.0 version
Method to check if headphones are plugged or any Bluetooth device with audio output connected
func bluetoothOrHeadphonesConnected() -> Bool {
let outputs = AVAudioSession.sharedInstance().currentRoute.outputs
for output in outputs{
if output.portType == AVAudioSessionPortBluetoothA2DP ||
output.portType == AVAudioSessionPortBluetoothHFP ||
output.portType == AVAudioSessionPortBluetoothLE ||
output.portType == AVAudioSessionPortHeadphones {
return true
}
}
return false
}
It's important to check if the headphones are plugged out while you listen any audio.
private func setupObservers() {
NotificationCenter.default.addObserver(self, selector: #selector(self.audioRouteChangeListener), name: .AVAudioSessionRouteChange, object: nil)
}
func audioRouteChangeListener(notification: Notification) {
guard let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as? Int else { return }
switch audioRouteChangeReason {
case AVAudioSessionRouteChangeReason.oldDeviceUnavailable.hashValue:
//plugged out
default:
break
}
}

[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(plugout:) name:AVAudioSessionRouteChangeNotification object:nil];
-(void)plugout:(NSNotification*)notification
{
isRemovedHeadset = YES;
}
and handle your code using this isRemovedHeadset boolean in your
if (moviePlayer.playbackState == MPMoviePlaybackStatePaused)
{
if(isRemovedHeadset)
{
isRemovedHeadset = NO;
[moviePlayer prepareToPlay];
[moviePlayer play];
return;
}
}

#Sajjon solution on Swift 5 with RxSwift
func handleRouteChange(_ notification: Notification) {
guard
let userInfo = notification.userInfo,
let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber,
let reason = AVAudioSession.RouteChangeReason(rawValue: reasonRaw.uintValue)
else { fatalError("Strange... could not get routeChange") }
switch reason {
case .oldDeviceUnavailable:
print("oldDeviceUnavailable")
case .newDeviceAvailable:
print("newDeviceAvailable")
if AVAudioSession.isHeadphonesConnected {
print("Just connected headphones")
}
case .routeConfigurationChange:
print("routeConfigurationChange")
case .categoryChange:
print("categoryChange")
default:
print("not handling reason")
}
}
func listenForNotifications() {
NotificationCenter.default.rx
.notification(AVAudioSession.routeChangeNotification)
.subscribe(onNext: { (n) in
self.handleRouteChange(n)
})
.disposed(by: disposeBag)
}

Related

AVAudioSession.routeChangeNotification Bluetooth Earphones Disconnect

I have a project where I am trying to get my bluetooth headphones to work. In my view controller I add routeChangeNotification to see what is going on and it fires twice (both with case .categoryChange. The first time it shows my bluetooth headphones and then the second time it fires it reverts back to device microphone.
In AppDelegate I have:
let session = AVAudioSession.sharedInstance()
do {
// https://stackoverflow.com/questions/51010390/avaudiosession-setcategory-swift-4-2-ios-12-play-sound-on-silent
if #available(iOS 10.0, *) {
try session.setCategory(.playAndRecord, mode: .default, options: [.defaultToSpeaker, .allowBluetooth])
} else {
session.perform(NSSelectorFromString("setCategory:withOptions:error:"), with: AVAudioSession.Category.playAndRecord, with: [
AVAudioSession.CategoryOptions.allowBluetooth,
AVAudioSession.CategoryOptions.defaultToSpeaker]
)
try session.setMode(.default)
}
try session.setActive(true)
} catch {
print(error)
}
In my ViewController I have:
#objc private func didRouteChangeNotification(_ notification: Notification) {
guard let userInfo = notification.userInfo,
let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt, let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else {
return
}
switch reason {
case .newDeviceAvailable:
debugPrint("newDeviceAvailable")
case .oldDeviceUnavailable:
debugPrint("oldDeviceUnavailable")
case .categoryChange:
debugPrint("catgoryChange")
default: ()
}
}

GKTurnBasedEventListener could not be set to delegate of my ViewController?

In objC the syntax written by Rawendrich for GKTurnBasedEventListener, which was GKTurnBasedEventHandler there at that time, now changed by Apple is as below.
if (!gameCenterAvailable) return;
void (^setGKEventHandlerDelegate)(NSError *) = ^ (NSError *error)
{
GKTurnBasedEventHandler *ev =
[GKTurnBasedEventHandler sharedTurnBasedEventHandler];
ev.delegate = self;
};
NSLog(#"Authenticating local user...");
if ([GKLocalPlayer localPlayer].authenticated == NO) {
[[GKLocalPlayer localPlayer]
authenticateWithCompletionHandler:
setGKEventHandlerDelegate];
} else {
NSLog(#"Already authenticated!");
setGKEventHandlerDelegate(nil);
}
Now after converting this to swift, and with the composition of writing down GKTurnBasedEventListener instead of GKTurnBasedEventHandler, this comes the following way.
// Converted with Swiftify v1.0.6381 - https://objectivec2swift.com/
if !gameCenterAvailable {
return
}
var setGKEventHandlerDelegate: ((_: Error) -> Void)? = {(_ error: Error?) -> Void in
var ev = GKTurnBasedEventHandler.shared()
ev.delegate = self
}
print("Authenticating local user...")
if GKLocalPlayer.localPlayer().authenticated == false {
GKLocalPlayer.localPlayer().authenticate(withCompletionHandler: setGKEventHandlerDelegate)
}
else {
print("Already authenticated!")
setGKEventHandlerDelegate(nil)
}
Unfortunately this is not the right syntax to set delegate of GKTurnBasedEventListener for my ViewController.
Please if anyone of you could solve this for me, because without this I'm not able to read through event listener's default functions.
Cheers!
FYI, if you want a working example of how to use GKLocalPlayerListener during a turn-based GameKit match, you are welcome to take a look at this example project for a turn based game. I hope it helps to see all the pieces in context.
Finally after just about harsh 10 hours, I figured out this problem from Here. Although this syntax is in objC, but there's no problem of converting it to swift from Swiftify.
Although a bit later than the real time, but I'm now able to understand that setting delegate of GKTunBasedEventListener is not like the one we do for UITableViewControllerDelegate.
Here, one must have to first authenticate local player, then after you have to register local player's listener to the ViewController's delegate GKLocalPlayerListener.
One other thing I found on Apple's Documentation:
Do not implement GKChallengeListener, GKInviteEventListener, GKSavedGameListener, and GKTurnBasedEventListener directly; implement GKLocalPlayerListener instead. You can listen for and handle multiple events using GKLocalPlayerListener.
So then on I've implemented in the following way.
import GameKit
class ViewController: UIViewController, GKTurnBasedMatchmakerViewControllerDelegate,
GKLocalPlayerListener {
.....
func player(_ player: GKPlayer, receivedTurnEventFor match: GKTurnBasedMatch, didBecomeActive: Bool) {
print("#1")
print(player)
print("#2")
print(match)
print("#3")
print(didBecomeActive)
if match.status == GKTurnBasedMatchStatus.open
{
if GKLocalPlayer.localPlayer() == match.currentParticipant
{
if didBecomeActive
{
// Active now
}
else
{
// Active already
}
}
else
{
// It's someone's turn
if match.matchData != myMatch?.matchData
{
// Match Data being Updated by Someone
print(player.alias ?? "No Name:")
}
}
}
thirdTopLabel.text = match.matchID! + "\n" + didBecomeActive.description
}
....
Now in ViewDidLoad() function put the following code.
// In the ViewDidLoad function
if(!GKLocalPlayer.localPlayer().isAuthenticated)
{
authenticatePlayer { (auth) in
weak var weakSelf = self
weak var weakPlayer = GKLocalPlayer.localPlayer()
if(auth){
weakPlayer?.register(weakSelf!)
self.suthentication = true;
}
else{
print("failed in authentication")
self.suthentication = false;
}
}
}
else
{
// Already Authenticated
GKLocalPlayer.localPlayer().register(self)
localPlayer = GKLocalPlayer.localPlayer()
}
And finally your Authentication function should be like this.
// authenticate local player :: Just Authentication
func authenticatePlayer(completionHandler: #escaping (_ resultedPlaces: Bool) -> Void) {
localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler =
{ (viewController , error ) -> Void in
if viewController != nil
{
self.present(viewController!, animated:true, completion: nil)
}
else
{
if self.localPlayer.isAuthenticated
{
completionHandler(true);
}
else
{
completionHandler(false);
print("not able to authenticate fail")
self.gameCenterEnabled = false
if (error != nil)
{
print("\(error.debugDescription)")
}
else
{
print( "error is nil")
}
}
}
}
}
NOTE: GKLocalPlayerListener won't work on simulator.

Sinch Video Call get Notification for Audio Call

I'm getting "Incoming audio call" however the call is Video
Hint: I'm using (IOS SDK 3.10.1) SDK and SinchService.
here is the code:
func startVideoCall(userId: String, isPrivate: Bool) -> SINCall? {
if let user = User.current {
let callingId = userId
if let callClient = self.callClient() {
let privateString = isPrivate ? VoipCallConstants.Parameters.PrivateTrue : VoipCallConstants.Parameters.PrivateFalse
let headers:[String: String] = [VoipCallConstants.Parameters.Private:privateString]
let call = callClient.callUserVideo(withId: callingId, headers: headers)
return call
}
}
return nil
}
func callClient() -> SINCallClient? {
let appDelegate: AppDelegate = (UIApplication.shared.delegate as! AppDelegate)
return appDelegate.sinch?.callClient()
}
class AppDelegate: UIResponder, UIApplicationDelegate{
var sinch: SINService!
var push: SINManagedPush!
}
and i added in SINSLazyCallClient this
- (id<SINCall>)callUserVideoWithId:(NSString *)userId {
return [self callUserVideoWithId:userId headers:#{}];
}
- (id<SINCall>)callUserVideoWithId:(NSString *)userId headers:(NSDictionary *)headers {
if (self.proxee) {
return [self.proxee callUserVideoWithId:userId headers:headers];
} else {
return [[SINSFailedCall alloc] initWithUserId:userId headers:headers];
}
}
here is Localization.strings
SIN_INCOMING_CALL = "Incoming audio call";
SIN_INCOMING_CALL_DISPLAY_NAME = "Incoming audio call from %#";
SIN_INCOMING_VIDEO_CALL = "Incoming video call";
SIN_INCOMING_VIDEO_CALL_DISPLAY_NAME = "Incoming video call from %#";
Thanks for your feedback. We looked into this and it turned out the support for SIN_INCOMING_VIDEO_CALL and SIN_INCOMING_VIDEO_CALL_DISPLAY_NAME is not implemented in our system yet. We will keep you updated once this is fixed.

AFNetworking Reachability Manager in Swift

Is there a way in Swift that the AFNetworking Reachability will continuously checking the internet connection every second, so far this is what I have and it only check one time only:
override func viewDidLoad() {
AFNetworkReachabilityManager.sharedManager().startMonitoring()
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock{(status: AFNetworkReachabilityStatus?) in
switch status!.hashValue {
case AFNetworkReachabilityStatus.NotReachable.hashValue:
print("no internet")
case AFNetworkReachabilityStatus.ReachableViaWiFi.hashValue,AFNetworkReachabilityStatus.ReachableViaWWAN.hashValue:
print("with internet")
default:
print("unknown")
}
}
}
How to check internet connection continuously ?
AFNetworking Reachability does check continuously the connection, I'm using it if a few of my apps and it does the job well. If your code is not working, I believe it might be because you call startMonitoring before setting the handler with setReachabilityStatusChangeBlock, so you might miss an initial event. Also, unrelated to your problem but an improvement you can make, you don't need to use the hashValue in the switch statement, you can use statusdirectly, and you get the benefit of the Swift compiler checking for the completion of the case statements. In summary, give a try to the following version of your code and see if it works:
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock { (status: AFNetworkReachabilityStatus) -> Void in
switch status {
case .NotReachable:
print("Not reachable")
case .ReachableViaWiFi, .ReachableViaWWAN:
print("Reachable")
case .Unknown:
print("Unknown")
}
}
AFNetworkReachabilityManager.sharedManager().startMonitoring()
You should not check for rechability for every minute or periodically. It's not good practice, it's decrease the performance of app.
You can get rechability change notifications. so when rechabilty change you can perform some task
You can do something like this,
You must create a Reachability object before you can receive notifications from it. Also, be sure to call the startNotifier() method on the Reachability object you create. This would be an example of how to do so inside of your application delegate:
class AppDelegate: UIResponder, UIApplicationDelegate
{
private var reachability:Reachability!;
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool
{
NSNotificationCenter.defaultCenter().addObserver(self, selector:"checkForReachability:", name: kReachabilityChangedNotification, object: nil);
self.reachability = Reachability.reachabilityForInternetConnection();
self.reachability.startNotifier();
}
func checkForReachability(notification:NSNotification)
{
// Remove the next two lines of code. You cannot instantiate the object
// you want to receive notifications from inside of the notification
// handler that is meant for the notifications it emits.
//var networkReachability = Reachability.reachabilityForInternetConnection()
//networkReachability.startNotifier()
let networkReachability = notification.object as Reachability;
var remoteHostStatus = networkReachability.currentReachabilityStatus()
if (remoteHostStatus.value == NotReachable.value)
{
println("Not Reachable")
}
else if (remoteHostStatus.value == ReachableViaWiFi.value)
{
println("Reachable via Wifi")
}
else
{
println("Reachable")
}
}
}
You can download reachability class from Apple Documentation
Hope this will help :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// internetReachable is declared as property.
internetReachable = [Reachability reachabilityForInternetConnection];
[internetReachable startNotifier];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(checkNetworkStatus:) name:kReachabilityChangedNotification object:nil];
}
- (void)checkNetworkStatus:(NSNotification *)notice {
// called when network status is changed
NetworkStatus internetStatus = [internetReachable currentReachabilityStatus];
switch (internetStatus)
{
case NotReachable:
{
NSLog(#"The internet is down.");
break;
}
case ReachableViaWiFi:
{
NSLog(#"The internet is Connected.");
break;
}
case ReachableViaWWAN:
{
NSLog(#"The internet is working via WWAN!");
break;
}
}
//#import "Reachability.m"
static void ReachabilityCallback(SCNetworkReachabilityRef target, SCNetworkReachabilityFlags flags, void* info)
{
#pragma unused (target, flags)
NSCAssert(info != NULL, #"info was NULL in ReachabilityCallback");
NSCAssert([(__bridge NSObject*) info isKindOfClass: [Reachability class]], #"info was wrong class in ReachabilityCallback");
Reachability* noteObject = (__bridge Reachability *)info;
// Post a notification to notify the client that the network reachability changed.
[[NSNotificationCenter defaultCenter] postNotificationName: kReachabilityChangedNotification object: noteObject];
}
You can utilize - https://github.com/ashleymills/Reachability.swift
//declare this inside of viewWillAppear
do {
reachability = try Reachability.reachabilityForInternetConnection()
} catch {
print("Unable to create Reachability")
return
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "reachabilityChanged:",name: ReachabilityChangedNotification,object: reachability)
do{
try reachability?.startNotifier()
}catch{
print("could not start reachability notifier")
}
//declare this inside of viewWillDisappear
reachability!.stopNotifier()
NSNotificationCenter.defaultCenter().removeObserver(self,
name: ReachabilityChangedNotification,
object: reachability)
func reachabilityChanged(note: NSNotification) {
let reachability = note.object as! Reachability
if reachability.isReachable() {
print("NETWORK REACHABLE.")
}
if reachability.isReachableViaWiFi()
{
print("NETWORK REACHABLE VIA WIFI.")
}
if reachability.isReachableViaWWAN()
{
print("NETWORK REACHABLE VIA WWAN.")
}
else {
print("NETWORK NOT REACHABLE.")
}
}
Also for my own archiving purposes. You can use this one as #Lion in his/her answer suggests.
Just call Reachability.registerListener() inside didFinishLaunchingWithOptions in AppDelegate.swift. It will automatically inform you on changes.
//
// CheckInternet.swift
//
// Created by Dincer on 14/11/15.
//
import AFNetworking
public class Reachability {
private static let theSharedInstance:Reachability = Reachability();
private var isClientOnline:Bool = true;
private var isClientWiFi:Bool = false;
private var isClientConnectionUnknown = false;
func onOnline() {
print("****************************************** Network goes online.");
}
func onOffline() {
print("****************************************** Network goes offline.");
}
func onWiFi() {
print("****************************************** Wifi network on");
}
func onGSM() {
print("****************************************** GSM network on");
}
func onUnknownConnectionStatus() {
print("****************************************** Unkown network status");
}
func isConnectedToNetwork() -> Bool {
return isClientOnline;
}
func isConnectedToWiFi() -> Bool {
return isClientOnline && isClientWiFi;
}
static func sharedInstance() -> Reachability {
return Reachability.theSharedInstance;
}
static func registerListener() {
sharedInstance().registerListener();
}
func registerListener() {
AFNetworkReachabilityManager.sharedManager().setReachabilityStatusChangeBlock { (status: AFNetworkReachabilityStatus) -> Void in
switch status {
case .NotReachable:
self.isClientConnectionUnknown = false;
if self.isClientOnline {
self.isClientOnline = false;
self.onOffline();
}
case .ReachableViaWiFi:
self.isClientConnectionUnknown = false;
if !self.isClientOnline {
self.isClientOnline = true;
self.onOnline();
}
if !self.isClientWiFi {
self.isClientWiFi = true;
self.onWiFi();
}
case .ReachableViaWWAN:
self.isClientConnectionUnknown = false;
if !self.isClientOnline {
self.isClientOnline = true;
self.onOnline();
}
if self.isClientWiFi {
self.isClientWiFi = false;
self.onGSM();
}
case .Unknown:
if !self.isClientConnectionUnknown {
self.isClientConnectionUnknown = true;
self.onUnknownConnectionStatus();
}
}
}
AFNetworkReachabilityManager.sharedManager().startMonitoring();
}
}

How can I detect external microphone in iOS?

How can I detect, from within my application, if an external microphone is plugged in the device?
Try this:
let route = AVAudioSession.sharedInstance().currentRoute
for port in route.outputs {
if port.portType == AVAudioSessionPortHeadphones {
// Headphones located
}
}
EDIT: Post OP change in question -
When app is running you need to register for AVAudioSessionRouteChangeNotification to listen to the changes like this:
NSNotificationCenter.defaultCenter().addObserver(self, selector:"audioRouteChangeListener:", name: AVAudioSessionRouteChangeNotification, object: nil)
dynamic private func audioRouteChangeListener(notification:NSNotification) {
let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as UInt
switch audioRouteChangeReason {
case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue:
println("headphone plugged in")
case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue:
println("headphone pulled out")
default:
break
}
}
swift3:
NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChangeListener(notification:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)
#objc private func audioRouteChangeListener(notification: Notification) {
let rawReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt
let reason = AVAudioSessionRouteChangeReason(rawValue: rawReason)!
switch reason {
case .newDeviceAvailable:
print("headphone plugged in")
case .oldDeviceUnavailable:
print("headphone pulled out")
default:
break
}
}
With swift 2.0 this code works
func audioRouteChangeListenerCallback (notif: NSNotification){
let userInfo:[NSObject:AnyObject] = notif.userInfo!
let routChangeReason = UInt((userInfo[AVAudioSessionRouteChangeReasonKey]?.integerValue)!)
switch routChangeReason {
case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue:
print("Connected");
break;
case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue:
do {
try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker)
} catch _ {
}
print("Connected");
break;
case AVAudioSessionRouteChangeReason.CategoryChange.rawValue:
break;
default:
break;
}
}
With AVAudioSession you can list the availableInputs
let session = AVAudioSession.sharedInstance()
_ = try? session.setCategory(AVAudioSessionCategoryRecord, withOptions: [])
print(AVAudioSession.sharedInstance().availableInputs)
It return an array of AVAudioSessionPortDescription. And you can have the portType "wired or builtIn" microphone type.
PS : It only work on real device not on simulator.

Resources