Alternative to AVSystemController_AudioVolumeNotificationPar



响应设备音量水平变化的批准KVO方法在达到min/max outputVolume后停止检测音量按钮按下。我希望在min/max之后继续收到那些按键事件,所以我认为我需要尝试这个解决方案,即使它不被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。这个组件工作起来非常出色,并且使用了苹果认可的KVO技术。请注意,该组件将AVAudioSession选项设置为. mixwithothers,这会阻止MPRemoteCommandCenter接收/处理任何蓝牙命令。所以如果你需要BT (Swift 5):

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

我还发现,在初始化按钮处理程序之前,通过编程将设备音量设置为0.5可以避免偶尔出现最小/最大障碍。如果设备初始音量接近最小或最大,处理程序将在按几个按钮后停止:

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

相关内容

  • 没有找到相关文章