Detect volume button press

后端 未结 4 1062
无人及你
无人及你 2020-12-01 09:44

Volume button notification function is not being called.

Code:

func listenVolumeButton(){
    // Option #1
    NSNotificationCenter.defaultCenter().a         


        
4条回答
  •  有刺的猬
    2020-12-01 10:23

    With this code you can listen whenever the user taps the volume hardware button.

    class VolumeListener {
        static let kVolumeKey = "volume"
    
        static let shared = VolumeListener()
    
        private let kAudioVolumeChangeReasonNotificationParameter = "AVSystemController_AudioVolumeChangeReasonNotificationParameter"
        private let kAudioVolumeNotificationParameter = "AVSystemController_AudioVolumeNotificationParameter"
        private let kExplicitVolumeChange = "ExplicitVolumeChange"
        private let kSystemVolumeDidChangeNotificationName = NSNotification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification")
    
        private var hasSetup = false
    
        func start() {
            guard !self.hasSetup else {
                return
            }
    
            self.setup()
            self.hasSetup = true
    
        }
    
        private func setup() {
            guard let rootViewController = UIApplication.shared.windows.first?.rootViewController else {
                return
            }
    
            let volumeView = MPVolumeView(frame: CGRect.zero)
            volumeView.clipsToBounds = true
            rootViewController.view.addSubview(volumeView)
    
            NotificationCenter.default.addObserver(
                self,
                selector: #selector(self.volumeChanged),
                name: kSystemVolumeDidChangeNotificationName,
                object: nil
            )
    
            volumeView.removeFromSuperview()
        }
    
        @objc func volumeChanged(_ notification: NSNotification) {
            guard let userInfo = notification.userInfo,
                let volume = userInfo[kAudioVolumeNotificationParameter] as? Float,
                let changeReason = userInfo[kAudioVolumeChangeReasonNotificationParameter] as? String,
                changeReason == kExplicitVolumeChange
                else {
                    return
            }
    
            NotificationCenter.default.post(name: "volumeListenerUserDidInteractWithVolume", object: nil,
                                            userInfo: [VolumeListener.kVolumeKey: volume])
        }
    }
    

    And to listen you just need to add the observer:

    NotificationCenter.default.addObserver(self, selector: #selector(self.userInteractedWithVolume),
                                               name: "volumeListenerUserDidInteractWithVolume", object: nil)
    

    You can access the volume value by checking the userInfo:

    @objc private func userInteractedWithVolume(_ notification: Notification) {
        guard let volume = notification.userInfo?[VolumeListener.kVolumeKey] as? Float else {
            return
        }
    
        print("volume: \(volume)")
    }
    

提交回复
热议问题