在iOS 14上从未触发SystemVolumeDidChangeNotification
目标是监视卷更改事件及其值。跟着导游走 在iOS 14上。*当我尝试观察卷更改事件时,该事件似乎在卷更改后从未被触发在iOS 14上从未触发SystemVolumeDidChangeNotification,ios,swift,nsnotificationcenter,ios14,Ios,Swift,Nsnotificationcenter,Ios14,目标是监视卷更改事件及其值。跟着导游走 在iOS 14上。*当我尝试观察卷更改事件时,该事件似乎在卷更改后从未被触发 import MediaPlayer override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) NotificationCenter.default.addObserver(self, selector: #selector(volumeChange(_:)), n
import MediaPlayer
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(volumeChange(_:)), name: Notification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
}
@objc func volumeChange(_ notification: NSNotification) {
let userInfo = notification.userInfo!
let volume = userInfo["AVSystemController_AudioVolumeNotificationParameter"] as! Double
print("volume:\(volume)")
}
beginReceivingRemoteControlEvents
需要首先调用
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
UIApplication.shared.beginReceivingRemoteControlEvents() // <- THIS LINE
NotificationCenter.default.addObserver(self, selector: #selector(volumeChange(_:)), name: Notification.Name(rawValue: "AVSystemController_SystemVolumeDidChangeNotification"), object: nil)
}
覆盖函数视图显示(uu动画:Bool){
super.viewdide显示(动画)
UIApplication.shared.beginReceivingRemoteControlEvents()//谢谢,我想知道为什么在iOS 14.1之前我们从来不需要使用它。