在iOS 7中检测(收听)音频路由更改

在iOS 7中检测(收听)音频路由更改,ios,audio,callback,audiosession,Ios,Audio,Callback,Audiosession,刚开始开发iOS 7,发现与AudioSession相关的函数和PropertyListener在iOS 7中不受欢迎 在使用以下方法检测耳机是否已插入或拔出设备之前: /* add callback for device route change */ AudioSessionAddPropertyListener ( kAudioSessionProperty_AudioRouteChange,

刚开始开发iOS 7,发现与AudioSession相关的函数和PropertyListener在iOS 7中不受欢迎

在使用以下方法检测耳机是否已插入或拔出设备之前:

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

然后实现侦听器回调,对内部算法执行不同的操作。现在iOS 7已经弃用了它,并且没有关于任何替代方案的文档,这里有专家提供的解决方案吗?谢谢

处理通知AVAudioSessionRouteChangeNotification(在iOS 6.0及更高版本中提供)。

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)
    }
}
并在
func setupAudioSession()中调用它

对于
目标C
请尝试此代码

- (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);

}
并在
(void)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)

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

非常感谢你的指点!稍后在本页中还发现了更多细节:这段代码是否也适用于iOS8?我正在尝试收听音量变化。“对象:sessionInstance”救了我一命。谢谢!