Headphones plugin/out detection in Swift

2019-03-22 06:45发布

im working on an iphone app for iOS 8.1 that works with core audio to generate frequencies and adjust intensities. In the view controller that i generate the frequencies i need to control if the headphones are plugged out in some moment, i'm already controlling if headphones are connected before proceed to my frequencies generator view with the following function:

- (BOOL)isHeadsetPluggedIn {
    AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance]   currentRoute];
    for (AVAudioSessionPortDescription* desc in [route outputs]) {
        if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
            return YES;
    }
    return NO;
}

this function is in C because im working with core-audio to generate the frequencies, but in the view controllers im working with swift so a need a way to implement a listener to detect the headphones plug-out event and return to the user to the previous view, i don't know if i can use my function isHeadsetPluggedin() with an event listener or i should make a new one. In my MenuViewController i control if the headphones are plugged in using the following function:

func isHeadsetPluggedIn() -> Bool {
    return freqController.isHeadsetPluggedIn();
}     

3条回答
在下西门庆
2楼-- · 2019-03-22 07:32

You can track the route changes by observing AVAudioSessionRouteChangeNotification notification.

//Observe for route changing notification
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handleRouteChange:) name:AVAudioSessionRouteChangeNotification object:[AVAudioSession sharedInstance]];

   -(void)handleRouteChange:(NSNotification *)notif
    {
       NSDictionary *dict = notif.userInfo;
       AVAudioSessionRouteDescription *routeDesc = dict[AVAudioSessionRouteChangePreviousRouteKey];
       AVAudioSessionPortDescription *prevPort = [routeDesc.outputs objectAtIndex:0];
       if ([prevPort.portType isEqualToString:AVAudioSessionPortHeadphones]) {
            //Head phone removed
          }
     }
查看更多
Deceive 欺骗
3楼-- · 2019-03-22 07:32

In Swift 4

    func activateHeadPhonesStatus(){
     NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChangeListener(_:)), name: AVAudioSession.routeChangeNotification, object: nil)
    }

    @objc func audioRouteChangeListener(_ 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:
                let session = AVAudioSession.sharedInstance()
                for output in session.currentRoute.outputs where output.portType == AVAudioSession.Port.headphones {
                    headphonesConnected = true
                    print("headphone plugged in")
                    break
                }
            case .oldDeviceUnavailable:
                if let previousRoute =
                    userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
                    for output in previousRoute.outputs where output.portType == AVAudioSession.Port.headphones {
                        headphonesConnected = false
                        print("headphone pulled out")
                        break
                    }
                }
            default: ()
            }

        }
查看更多
对你真心纯属浪费
4楼-- · 2019-03-22 07:36

This article worked for me. There is also a GitHub repo with solution. If you don't want to read, here is my code:

Put this in your INIT method:

    self.session = AVAudioSession.sharedInstance()
    let currentRoute = self.session.currentRoute
    if currentRoute.outputs.count != 0 {
        for description in currentRoute.outputs {
            if description.portType == AVAudioSessionPortHeadphones {
                print("headphone plugged in")
            } else {
                print("headphone pulled out")
            }
        }
    } else {
        print("requires connection to device")
    }

    NSNotificationCenter.defaultCenter().addObserver(
        self,
        selector: #selector(YOUR_VIEW_CONTROLLER_OR_VIEW.audioRouteChangeListener(_:)),
        name: AVAudioSessionRouteChangeNotification,
        object: nil)

And put this anywhere in your class:

 dynamic private func audioRouteChangeListener(notification:NSNotification) {
    let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt

    switch audioRouteChangeReason {
    case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue:
        print("headphone plugged in")
    case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue:
        print("headphone pulled out")
    default:
        break
    }
}

Take care!

查看更多
登录 后发表回答