Detect (listen to) audio route change in iOS 7
Asked Answered
T

2

21

Just starting development for iOS 7, and found that AudioSession related functions and PropertyListeners are deprecated in iOS 7.

Before I use the following method to detect if a headset has been plugged in or unplugged from the device:

    /* add callback for device route change */
    AudioSessionAddPropertyListener (
                                     kAudioSessionProperty_AudioRouteChange,
                                     audioRouteChangeListenerCallback,
                                     (__bridge void *)(self));

Then implement the listener callback to do different things to the internal algorithms. Now iOS 7 deprecated it and there's no documentations on any alternative, Is there any solutions by experts here? Thanks!

Triplicate answered 29/10, 2013 at 5:42 Comment(0)
E
26

Handle the notification AVAudioSessionRouteChangeNotification (Available in iOS 6.0 and later.)

Evonevonne answered 29/10, 2013 at 5:52 Comment(2)
Thanks a lot for the pointer! Later on also found more detail in this page: github.com/adriantofan/SoundWaveVisualiser/blob/master/DemoApp/…Triplicate
Should this code also work in iOS8? I am trying to listen for volume change.Brey
L
7

Try this code for Swift 4.2 :

@objc func handleRouteChange(_ notification: Notification) {
    let reasonValue = (notification as NSNotification).userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt
    let routeDescription = (notification as NSNotification).userInfo![AVAudioSessionRouteChangePreviousRouteKey] as! AVAudioSessionRouteDescription?
    
    NSLog("Route change:")
    if let reason = AVAudioSession.RouteChangeReason(rawValue: reasonValue) {
        switch reason {
        case .newDeviceAvailable:
            NSLog("     NewDeviceAvailable")
        case .oldDeviceUnavailable:
            NSLog("     OldDeviceUnavailable")
        case .categoryChange:
            NSLog("     CategoryChange")
            NSLog(" New Category: %@", AVAudioSession.sharedInstance().category.rawValue)
        case .override:
            NSLog("     Override")
        case .wakeFromSleep:
            NSLog("     WakeFromSleep")
        case .noSuitableRouteForCategory:
            NSLog("     NoSuitableRouteForCategory")
        case .routeConfigurationChange:
            NSLog("     RouteConfigurationChange")
        case .unknown:
            NSLog("     Unknown")
        @unknown default:
            NSLog("     UnknownDefault(%zu)", reasonValue)
        }
    } else {
        NSLog("     ReasonUnknown(%zu)", reasonValue)
    }
    
    if let prevRout = routeDescription {
        NSLog("Previous route:\n")
        NSLog("%@", prevRout)
        NSLog("Current route:\n")
        NSLog("%@\n", AVAudioSession.sharedInstance().currentRoute)
    }
}

And use it in func setupAudioSession()

    private func setupAudioSession() {

       // Configure the audio session
       let sessionInstance = AVAudioSession.sharedInstance()
        
       // we don't do anything special in the route change notification
       NotificationCenter.default.addObserver(self,
           selector: #selector(self.handleRouteChange(_:)),
           name: AVAudioSession.routeChangeNotification,
           object: sessionInstance)

}

For Objective C try this code

- (void)handleRouteChange:(NSNotification *)notification
{
    UInt8 reasonValue = [[notification.userInfo valueForKey:AVAudioSessionRouteChangeReasonKey] intValue];
    AVAudioSessionRouteDescription *routeDescription = [notification.userInfo valueForKey:AVAudioSessionRouteChangePreviousRouteKey];

    NSLog(@"Route change:");
    switch (reasonValue) {
        case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
            NSLog(@"     NewDeviceAvailable");
            break;
        case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
            NSLog(@"     OldDeviceUnavailable");
            break;
        case AVAudioSessionRouteChangeReasonCategoryChange:
            NSLog(@"     CategoryChange");
            NSLog(@" New Category: %@", [[AVAudioSession sharedInstance] category]);
            break;
        case AVAudioSessionRouteChangeReasonOverride:
            NSLog(@"     Override");
            break;
        case AVAudioSessionRouteChangeReasonWakeFromSleep:
            NSLog(@"     WakeFromSleep");
            break;
        case AVAudioSessionRouteChangeReasonNoSuitableRouteForCategory:
            NSLog(@"     NoSuitableRouteForCategory");
            break;
        default:
            NSLog(@"     ReasonUnknown");
    }

    NSLog(@"Previous route:\n");
    NSLog(@"%@\n", routeDescription);
    NSLog(@"Current route:\n");
    NSLog(@"%@\n", [AVAudioSession sharedInstance].currentRoute);

}

And use it in (void)setupAudioSession

- (void)setupAudioSession {
    // Configure the audio session
    AVAudioSession *sessionInstance = [AVAudioSession sharedInstance];
    
    // we don't do anything special in the route change notification
    [[NSNotificationCenter defaultCenter] addObserver:self
                                          selector:@selector(handleRouteChange:)
                                          name:AVAudioSessionRouteChangeNotification
                                          object:sessionInstance];
}
Loretaloretta answered 17/4, 2019 at 15:17 Comment(3)
"object: sessionInstance" saved me. Thanks a lot!Klutz
Could you add how to remove the observer after adding it?Galleywest
#28690489 @MigueldeSousaLoretaloretta

© 2022 - 2024 — McMap. All rights reserved.