替代 AVSystemController_AudioVolumeNotificationParameter post iOS 15?

Alternative to AVSystemController_AudioVolumeNotificationParameter post iOS 15?

for responding to device volume level changes stops detecting volume button presses after min/max outputVolume is reached. I'd like to continue to receive those button press events after min/max, so I assume I need to try ,即使它不受 Apple 支持。但是,我是一个非常业余的 iOS 程序员,所以我可以使用提示。这是我一直在做的(使用 RxSwift):

NotificationCenter.default.rx.notification(Notification.Name(rawValue: "AVSystemController_AudioVolumeNotificationParameter"))
                .subscribe(onNext: { [weak self] notification in
                    guard let my = self else { return }
                    my.volumeNotification.accept(notification.userInfo!["AVSystemController_AudioVolumeNotificationParameter"] as! Double)
                })
                .disposed(by: disposeBag)

我是否应该订阅名为“MPVolumeControllerDataSource_SystemVolumeDidChange”的通知?

提前致谢!

为开源欢呼三声,具体来说:JPSVolumeButtonHandler。该组件工作得非常出色,并使用了 Apple 认可的 KVO 技术。请注意,此组件将 AVAudioSession 选项设置为 .mixWithOthers,这会阻止 MPRemoteCommandCenter receiving/handing 任何蓝牙命令。所以如果你需要BT(Swift 5):

let volumeButtonHandler = JPSVolumeButtonHandler(up: {
    // handle up press
}, downBlock: {
    // handle down press
})
volumeButtonHandler.sessionOptions = [] // allow remote BT

我还发现在初始化按钮处理程序之前以编程方式将设备音量设置为 0.5 避免了偶尔的 min/max 障碍。如果设备初始音量接近最小值或最大值,处理程序将在按下几次按钮后停止:

try AVAudioSession.sharedInstance().setActive(true, options: [])
MPVolumeView(frame: .zero).volumeSlider.value = 0.5