detect up volume change swift

后端 未结 1 1966
后悔当初
后悔当初 2020-12-19 12:56

I\'m having problems detecting when someone presses up or down volume button. For the moment I just play a file but I want to know when the user presses the button to show a

相关标签:
1条回答
  • 2020-12-19 13:37

    This should do the trick.

    class ViewController: UIViewController {
    
        // MARK: Properties
    
        let notificationCenter = NSNotificationCenter.defaultCenter()
    
        // MARK: Lifecycle
    
        override func viewDidAppear(animated: Bool) {
    
            super.viewDidAppear(animated)
    
            notificationCenter.addObserver(self,
                selector: #selector(systemVolumeDidChange),
                name: "AVSystemController_SystemVolumeDidChangeNotification",
                object: nil
            )
        }
    
        override func viewDidDisappear(animated: Bool) {
    
            super.viewDidDisappear(animated)
    
            notificationCenter.removeObserver(self)
        }
    
        // MARK: AVSystemPlayer - Notifications
    
        func systemVolumeDidChange(notification: NSNotification) {
    
            print(notification.userInfo?["AVSystemController_AudioVolumeNotificationParameter"] as? Float)
        }
    }
    
    0 讨论(0)
提交回复
热议问题