1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| 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{ } }
|