Detect volume button press

后端 未结 4 1162
无人及你
无人及你 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:22

    The code above won't work in Swift 3, in that case, try this:

    func listenVolumeButton() {
       do {
        try audioSession.setActive(true)
       } catch {
        print("some error")
       }
       audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil)
    }
    
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
      if keyPath == "outputVolume" {
        print("got in here")
      }
    }
    

提交回复
热议问题