问题描述
在 iOS 8 上按下音量增大/减小按钮的最佳/最干净方法是什么?
What's the best/cleanest way of capturing volume up/down button presses on iOS 8?
理想情况下,我想抓拍按键并防止系统音量改变(或至少阻止音量改变HUD显示)。
Ideally I'd like to capture the keypress and also prevent the system volume from changing (or at the very least, prevent the volume change HUD from showing).
有一些旧答案会使用已弃用的方法,但在iOS 8上似乎根本不起作用。这个也没有用。
There are some old answers going around which use deprecated methods and don't seem to work at all on iOS 8. This iOS 8 specific one didn't work either.
这个开源类似乎也无法在iOS 8上运行。
This RBVolumeButtons open source class doesn't seem to work on iOS 8 either.
推荐答案
首先添加 AVFoundation 和 MediaPlayer 框架,然后您可以使用以下代码检测上/下按钮按下,
First add AVFoundation and MediaPlayer Framework and then you can use below code to detect up/down button press,
-(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"]) {
float volumeLevel = [[MPMusicPlayerController applicationMusicPlayer] volume];
NSLog(@"volume changed! %f",volumeLevel);
}
}
这篇关于在iOS 8上按下音量上/下按钮的最简洁方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!