检测iPhone音量button按?

是否有通知,我可以听,这将告诉我什么时候iPhone的音量调高

我知道AVSystemController_SystemVolumeDidChangeNotification ,但重要的是,通知只能在音量调高时触发,而不是上调或下调。

其次,如何隐藏按下音量增大button时出现的半透明视图,显示系统音量? 相机+已经实现了这一点。

没有文档说明的方法,但可以使用此解决方法。 注册AVSystemController_SystemVolumeDidChangeNotification通知并添加一个MPVolumeView ,以防止系统卷视图出现。

 MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-100, 0, 10, 0)]; [volumeView sizeToFit]; [self.view addSubview:volumeView]; 

别忘了开始audio会议

 AudioSessionInitialize(NULL, NULL, NULL, NULL); AudioSessionSetActive(true); 

在这种情况下, MPVolumeView对用户是隐藏的。

至于检查音量是否上升或下降,只需抓住当前应用程序的音量

 float volumeLevel = [[MPMusicPlayerController applicationMusicPlayer] volume]; 

并在通知callback中按下button后将其与新音量进行比较

如果你不想自己做这个,那么github上有一个drop-in类

https://github.com/blladnar/RBVolumeButtons

如果你想要一个事件,你可以在“outputVolume”属性上注册一个监听器:

 - (void)viewWillAppear:(BOOL)animated { AVAudioSession* audioSession = [AVAudioSession sharedInstance]; [audioSession setActive:YES error:nil]; [audioSession addObserver:self forKeyPath:@"outputVolume" options:0 context:nil]; } -(void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqual:@"outputVolume"]) { NSLog(@"volume changed!"); } } 

我解决了这个问题,通过添加自己的目标/行动放置在MPVolumeView内的MPVolumeView 。 所以可以捕捉音量变化事件并确定按下了哪个button。 这里是github回购实施这种方法。 它适用于iOS 7及以上版本,没有弃用警告,也没有被苹果拒绝。

为了区分卷动作: INSTEAD OF (在观察值守卫中)

 temp != 0.5 

仅用于音量调节

 temp > 0.5 

只能检测到音量下降:

 temp < 0.5 

如果按下音量调高或调低音量,将打印下面的解决scheme。

 import AVFoundation import MediaPlayer override func viewDidLoad() { super.viewDidLoad() let volumeView = MPVolumeView(frame: CGRect.zero) for subview in volumeView.subviews { if let button = subview as? UIButton { button.setImage(nil, for: .normal) button.isEnabled = false button.sizeToFit() } } UIApplication.shared.windows.first?.addSubview(volumeView) UIApplication.shared.windows.first?.sendSubview(toBack: volumeView) } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) AVAudioSession.sharedInstance().addObserver(self, forKeyPath: "outputVolume", options: NSKeyValueObservingOptions.new, context: nil) do { try AVAudioSession.sharedInstance().setActive(true) } catch { debugPrint("\(error)") } } override func viewDidDisappear(_ animated: Bool) { super.viewDidDisappear(animated) AVAudioSession.sharedInstance().removeObserver(self, forKeyPath: "outputVolume") do { try AVAudioSession.sharedInstance().setActive(false) } catch { debugPrint("\(error)") } } override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { guard let key = keyPath else { return } switch key { case "outputVolume": guard let dict = change, let temp = dict[NSKeyValueChangeKey.newKey] as? Float, temp != 0.5 else { return } let systemSlider = MPVolumeView().subviews.first { (aView) -> Bool in return NSStringFromClass(aView.classForCoder) == "MPVolumeSlider" ? true : false } as? UISlider systemSlider?.setValue(0.5, animated: false) guard systemSlider != nil else { return } debugPrint("Either volume button tapped.") default: break } }