音量按鈕通知功能未被調用。檢測音量按鈕按
代碼:
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