本文介绍了检测音量按钮按下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



代码:

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

override func observeValueForKeyPath(keyPath:String,of object object:AnyObject,change:[NSObject:AnyObject],context:UnsafeMutablePointer< Void>){
如果keyPath ==volumeChanged{
print(get in here)
}
}

func volumeChanged(通知:NSNotification){
打印(在这里)
}

listenVolumeButton()正在viewWillAppear中调用



代码没有进入打印语句进入这里,in无论是哪种情况。



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



我有紧随其后: outputVolume。这是我们正在观察的财产。
所以更改代码,

  func listenVolumeButton(){

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

覆盖func observeValueForKeyPath(keyPath:String,ofObject对象:AnyObject,
更改:[NSObject:AnyObject],context:UnsafeMutablePointer< Void>){
if keyPath ==outputVolume{
print(get in here)
}
}


Volume button notification function is not being called.

Code:

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() is being called in viewWillAppear

The code is not getting to the print statement "got in here", in either case.

I am trying two different ways to do it, neither way is working.

I have followed this: Detect iPhone Volume Button Up Press?

解决方案

Using the second method, the value of the key path should be "outputVolume". That is the property we are observing.So change the code to,

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")
    }
}

这篇关于检测音量按钮按下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:51