SystemVolumeDidChangeNotification 永远不会在 iOS 14 上触发
SystemVolumeDidChangeNotification never get triggered on iOS 14
另一个标题是“为什么 AVSystemController_AudioVolumeNotificationParameter 通知事件永远不会被触发?”
目标是监控音量变化事件及其值。按照 的指南进行操作。
在 iOS 14.* 上,当我尝试观察音量变化事件时,该事件似乎永远不会在音量变化后触发。
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)
}
不要忘记在视图消失后移除观察者。
另一个标题是“为什么 AVSystemController_AudioVolumeNotificationParameter 通知事件永远不会被触发?”
目标是监控音量变化事件及其值。按照
在 iOS 14.* 上,当我尝试观察音量变化事件时,该事件似乎永远不会在音量变化后触发。
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)
}
不要忘记在视图消失后移除观察者。