Detect volume button press

后端 未结 4 1182
无人及你
无人及你 2020-12-01 09:44

Volume button notification function is not being called.

Code:

func listenVolumeButton(){
    // Option #1
    NSNotificationCenter.defaultCenter().a         


        
4条回答
  •  生来不讨喜
    2020-12-01 10:15

    Using the second method, the value of the key path should be "outputVolume". That is the property we are observing. So change the code to,

    var outputVolumeObserve: NSKeyValueObservation?
    let audioSession = AVAudioSession.sharedInstance()
    
    func listenVolumeButton() {
        do {
            try audioSession.setActive(true)
        } catch {}
    
        outputVolumeObserve = audioSession.observe(\.outputVolume) { (audioSession, changes) in
            /// TODOs
        }
    }
    

提交回复
热议问题