iOS听筒和外放切换

2019-10-22 14:35:46 浏览数 (1)

本文语法为Swift4

代码

主要涉及到距离传感器的调用

代码语言:javascript复制
func addProximityMonitoring(){
    UIDevice.current.isProximityMonitoringEnabled = true;
    if(UIDevice.current.isProximityMonitoringEnabled){
        NotificationCenter.default.addObserver(self, selector: #selector(ChatViewController.sensorStateChange(_:)), name: NSNotification.Name.UIDeviceProximityStateDidChange, object: nil);
    }
}
    
func removeProximityMonitoring(){
    UIDevice.current.isProximityMonitoringEnabled = true;
    if(UIDevice.current.isProximityMonitoringEnabled){
        UIDevice.current.isProximityMonitoringEnabled = false;
        NotificationCenter.default.removeObserver(self, name: NSNotification.Name.UIDeviceProximityStateDidChange, object: nil);
    }
}
    
@objc func sensorStateChange(_ notification:NotificationCenter){
    do{
        if(UIDevice.current.proximityState){
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayAndRecord);
        }else{
            try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback);
        }
    }catch{
        
    }
}

0 人点赞