繁体   English   中英

在 iOS 7 中检测(听)音频路由变化

[英]Detect (listen to) audio route change in iOS 7

刚开始为iOS 7开发,发现iOS 7中不推荐使用AudioSession相关的函数和PropertyListeners。

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

    /* 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()调用它

    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)

}

对于Objective 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调用它

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM