2015-02-12 194 views
12

音量按钮通知功能未被调用。检测音量按钮按

代码:

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?

回答

18

使用第二种方法,关键路径的值应为"outputVolume"。这是我们正在观察的财产。 所以更改代码,

func listenVolumeButton(){ 

    let audioSession = AVAudioSession.sharedInstance() 
    audioSession.setActive(true, error: nil) 
    audioSession.addObserver(self, forKeyPath: "outputVolume", 
     options: NSKeyValueObservingOptions.New, context: nil) 
} 

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

干杯,我很感激帮助! – AustinT 2015-02-12 10:23:11

+3

当音量达到最大值时,是否仍可以获取通知? – AustinT 2015-02-12 10:24:33

+0

没有。它不可能使用这种方法。 – rakeshbs 2015-02-12 10:25:11

14

上面的代码不会在雨燕3工作,在这种情况下,试试这个:

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") 
    } 
} 
3
import AVFoundation 
import MediaPlayer 

override func viewDidLoad() { 
    super.viewDidLoad() 
    let volumeView = MPVolumeView(frame: CGRect.zero) 
    for subview in volumeView.subviews { 
    if let button = subview as? UIButton { 
     button.setImage(nil, for: .normal) 
     button.isEnabled = false 
     button.sizeToFit() 
    } 
    } 
    UIApplication.shared.windows.first?.addSubview(volumeView) 
    UIApplication.shared.windows.first?.sendSubview(toBack: volumeView) 
} 

override func viewWillAppear(_ animated: Bool) { 
    super.viewWillAppear(animated) 
    AVAudioSession.sharedInstance().addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil) 
    do { try AVAudioSession.sharedInstance().setActive(true) } 
    catch { debugPrint("\(error)") } 
} 

override func viewDidDisappear(_ animated: Bool) { 
    super.viewDidDisappear(animated) 
    AVAudioSession.sharedInstance().removeObserver(self, forKeyPath: "outputVolume") 
    do { try AVAudioSession.sharedInstance().setActive(false) } 
    catch { debugPrint("\(error)") } 
} 

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 
    guard let key = keyPath else { return } 
    switch key { 
    case "outputVolume": 
     guard let dict = change, let temp = dict[NSKeyValueChangeKey.newKey] as? Float, temp != 0.5 else { return } 
     let systemSlider = MPVolumeView().subviews.first { (aView) -> Bool in 
     return NSStringFromClass(aView.classForCoder) == "MPVolumeSlider" ? true : false 
    } as? UISlider 
     systemSlider?.setValue(0.5, animated: false) 
     guard systemSlider != nil else { return } 
     debugPrint("Either volume button tapped.") 
    default: 
     break 
    } 
} 

当观察一个新的价值,我设置系统音量回到0.5。这可能会激怒用户同时使用音乐,因此我不建议我在生产中自己的答案。