音量按钮通知功能未被调用。检测音量按钮按
代码:
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")
}
listenVolumeButton()
被称为viewWillAppear中
的代码是没有得到的打印语句"got in here"
,在这两种情况下。
我正在尝试两种不同的方式来做到这一点,两种方式都没有问题。
我都遵循这样的:Detect iPhone Volume Button Up Press?
干杯,我很感激帮助! – AustinT 2015-02-12 10:23:11
当音量达到最大值时,是否仍可以获取通知? – AustinT 2015-02-12 10:24:33
没有。它不可能使用这种方法。 – rakeshbs 2015-02-12 10:25:11