相关疑难解决方法(0)

检测音量按钮按下

未调用音量按钮通知功能.

码:

func listenVolumeButton(){
    // Option #1
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "volumeChanged:", name: "AVSystemController_SystemVolumeDidChangeNotification", object: nil)
    // Option #2
    var audioSession = AVAudioSession()
    audioSession.setActive(true, error: nil)
    audioSession.addObserver(self, forKeyPath: "volumeChanged", options: NSKeyValueObservingOptions.New, context: nil)
}

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
    if keyPath == "volumeChanged"{
        print("got in here")
    }
}

func volumeChanged(notification: NSNotification){
   print("got in here")
}
Run Code Online (Sandbox Code Playgroud)

listenVolumeButton() 正在viewWillAppear中调用

"got in here"在任何一种情况下,代码都没有进入print语句.

我正在尝试两种不同的方法来实现它,两种方式都不起作用.

我跟着这个:检测iPhone音量按钮向上按?

ios swift ios8

13
推荐指数
4
解决办法
1万
查看次数

标签 统计

ios ×1

ios8 ×1

swift ×1