iOS音量键翻页实现

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

iOS⾳量键翻页实现
前⾔:
最近⽼是有⽤户反馈说希望在阅读器⾥⾯加上⼀个⾳量键翻页的功能,作为⼀名多年的书⾍,实在是⽆法理解,产品也注意到了这个事情,故去研究了⼀下
稍微⾕歌了⼀下便可以知道,苹果以前是⽀持的,暴露了相关的api,在版本更新过程中移除了,要想监听物理⾳量键的按压事件,只能通过监听⼿机⾳量的变化,
但是⼜有⼀个问题是⾳量变化会在⼿机屏幕上有系统⾳量变化UI,这个当时随意看了下,觉得⽆法去除,遂拒绝了产品的需求。

最近突然⼜看到了类似的问题,且有⽤户提供了另外⼀款APP实现了该功能,遂深⼊了解下,话不多说,开始
1、获取⾳量监听
[kNotificationCenter addObserver:self selector:@selector(voliceChangeAction:) name:@"AVSystemController_SystemVolumeDidChangeNotification"object:nil];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
- (void)voliceChangeAction:(NSNotification *)sender {
NSDictionary *dict = erInfo;
double voiceNum = [[dict objectOrNilForKey:@"AVSystemController_AudioVolumeNotificationParameter"] doubleValue];
if (voiceNum > cashVoice) {
NSLog(@"⾳量加");
}
if (voiceNum < cashVoice) {
NSLog(@"⾳量减");
}
}
2、隐藏系统⾳量变化UI
此处有⼀点需要注意,系统⾳量变化的UI有两种情况,要分开处理
⼀是铃声⾳量变化;
⼆是媒体⾳量变化;
导⼊ #import <AVFoundation/AVFoundation.h>
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
if (audioSession.otherAudioPlaying) {
[audioSession setActive:NO error:&error];
} else {
[audioSession setActive:YES error:&error];
}
导⼊ MediaPlayer
#import <MediaPlayer/MPVolumeView.h>
在当前控制器视图上添加⼀个不在屏幕内的 MPVolumeView 对象即可隐藏掉系统媒体⾳量变化UI
MPVolumeView *volieView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-10, -100, 100, 100)];
[vc.view addSubview:volieView];
3、每次按完⾳量键之后需要重置⾳量,还是需要⽤到 MPVolumeView
UISlider *volumeViewSlider = nil;
for (UIView *view in [volieView subviews]) {
if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
volumeViewSlider = (UISlider *)view;
break;
}
}
self.volumeViewSlider = volumeViewSlider;
[self.volumeViewSlider setValue:0.25f animated:NO];
写的有点乱,不过主体代码都在这⾥了,稍微整理下进阅读器就可以实现这个功能
#import <MediaPlayer/MPVolumeView.h>
#import <AVFoundation/AVFoundation.h>
@property (nonatomic, strong) UISlider *volumeViewSlider;
//监听
[kNotificationCenter addObserver:self selector:@selector(voliceChangeAction:) name:@"AVSystemController_SystemVolumeDidChangeNotification"object:nil]; [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
AVAudioSession *audioSession = [AVAudioSession sharedInstance];
NSError *error = nil;
if (audioSession.otherAudioPlaying) {
[audioSession setActive:NO error:&error];
} else {
[audioSession setActive:YES error:&error];
}
- (void)voliceChangeAction:(NSNotification *)sender {
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
UIViewController *vc = [self getCurrentActivityViewController];
MPVolumeView *volieView = [[MPVolumeView alloc] initWithFrame:CGRectMake(-10, -100, 100, 100)];
[vc.view addSubview:volieView];
UISlider *volumeViewSlider = nil;
for (UIView *view in [volieView subviews]) {
if ([view.class.description isEqualToString:@"MPVolumeSlider"]) {
volumeViewSlider = (UISlider *)view;
break;
}
}
self.volumeViewSlider = volumeViewSlider;
[self.volumeViewSlider setValue:0.25f animated:NO];
[kUserDefaults setDouble:0.25f forKey:@"reader_VoiceNum"];
});
NSDictionary *dict = erInfo;
double voiceNum = [[dict objectOrNilForKey:@"AVSystemController_AudioVolumeNotificationParameter"] doubleValue];
double cashVoice = [kUserDefaults doubleForKey:@"reader_VoiceNum"];
if (voiceNum == 0.25f) {
return;
}
if (voiceNum > cashVoice) {
NSLog(@"⾳量加");
}
if (voiceNum < cashVoice) {
NSLog(@"⾳量减");
}
[kUserDefaults setDouble:0.25f forKey:@"reader_VoiceNum"];
//调整回⾳量
[self.volumeViewSlider setValue:0.25f animated:NO];
}。

相关文档
最新文档