【问题标题】:Detect (listen to) audio route change in iOS 7在 iOS 7 中检测(收听)音频路由变化
【发布时间】:2013-10-29 05:42:46
【问题描述】:

刚开始iOS 7的开发,发现iOS 7弃用了AudioSession相关的函数和PropertyListeners。

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

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

然后实现监听回调来对内部算法做不同的事情。现在 iOS 7 已弃用它,并且没有任何替代方案的文档,这里有专家的解决方案吗?谢谢!

【问题讨论】:

    标签: ios audio callback audiosession


    【解决方案1】:

    处理通知AVAudioSessionRouteChangeNotification(iOS 6.0 及更高版本可用。)

    【讨论】:

    【解决方案2】:

    试试这个代码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];
    }
    

    【讨论】:

    • "object: sessionInstance" 救了我。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-11
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多