Swift:Detect Volumn Button Press

From Hawk Wiki
Revision as of 04:16, 23 March 2015 by Hall (Talk | contribs) (Created page with "Credit to http://stackoverflow.com/questions/28471481/swift-detect-volume-button-press Detect volume button press, also hide volume HUD <pre class="brush:swift"> import Medi...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Credit to http://stackoverflow.com/questions/28471481/swift-detect-volume-button-press

Detect volume button press, also hide volume HUD

import MediaPlayer //Only for hidding  Volume view
func listenVolumeButton(){

    let audioSession = AVAudioSession.sharedInstance()
    audioSession.setActive(true, error: nil)
    audioSession.addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.New, context: nil)
    //If you want to hide Volume HUD view
    var volumeView: MPVolumeView = MPVolumeView(frame: CGRectZero)
    view.addSubview(volumeView)
}

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