监听音量键--展示出当前界面的所有请求信息

目的:监听音量键,在真机调试时展示接口返回的信息

注意点:

1.在DEBUG的时候才存储、展示(切记,否则当你发布出去的时候,靠,全暴露了,老大得掐死你)
2.只打印当前控制器的,信息比较多,给我前一个界面的没啥用
3.程序在后台的时候,点击音量别瞎处理
4.点击音量键的时候,别显示系统的音量增减的alert
5.区分我点击的是音量加还是音量减(纯属老大挑刺,所以考虑全了再跟老大说,否则你就得解决)

获取展示数据并存本地:

首先,拿到要展示的数据,并存到本地(累存)
      在请求的返回方法里你应该可以拿到请求的url、参数和返回的结果(responseObject或者error)  应该可以拿到4种:GET的成功、失败;POST的成功、失败
      可以将这4种信息存到本地:(以GET请求成功的信息为例)
        NSString *str  = [NSString stringWithFormat:@"GET_Success:==%@\n%@-----%@",url,params,responseObject];
        [SDGridItemCacheTool saveRequestAndVolumeKeyDEBUG:[NSArray arrayWithObject:str]];
当然,一个控制器中可能有N个请求,所以你需要把当前控制器的N个请求都累存到本地的数组中,存取方法如下:
+ (void)saveRequestAndVolumeKeyDEBUG:(NSArray *)requestData{
    NSString *path = [DOCUMENTPath stringByAppendingPathComponent:@"volumeKeyDEBUGData.data"];
    NSArray *arr = [self getRequestAndVolumeKeyDEBUG];
    if ([self getRequestAndVolumeKeyDEBUG]) {
        NSMutableArray *marr = [NSMutableArray arrayWithArray:arr];
[marr addObjectsFromArray:requestData];
        [NSKeyedArchiver archiveRootObject:marr toFile:path];
    }else{
        [NSKeyedArchiver archiveRootObject:requestData toFile:path];
    }

+ (NSArray *)getRequestAndVolumeKeyDEBUG{
    NSString *path = [DOCUMENTPath stringByAppendingPathComponent:@"volumeKeyDEBUGData.data"];
    NSArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return arr;
}

OK,都存下来了,然后就要展示出来了,在你的父控制器中(记住是父控制器,这个东西不需要写在每个控制器中)监听音量的变化,并做相应的处理
1.导入系统的AVFoundation/AVFoundation.h、MediaPlayer/MediaPlayer.h两个头文件

2.搞两个全局参数

/**是否进入后台,当为YES时不处理音量键*/
@property (nonatomic, assign)BOOL isIntoBg;
/**记录当前音量*/
@property (nonatomic, assign)float currentVolume;

3.在viewDidLoad里获取当前音量并监听音量键

#ifdef DEBUG
_currentVolume = [[AVAudioSession sharedInstance] outputVolume];
NSLog(@"当前的音量是: %f", _currentVolume);
//监听音量键
[self listenToAudioSession];
#endif

4.剩下的代码

//===========================================================监听音量键的处理=============================================================
- (void)listenToAudioSession{
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setActive:YES error:nil];
    [session setCategory:AVAudioSessionCategoryPlayback error:nil];
    NSError *error;
    [[AVAudioSession sharedInstance] setActive:YES error:&error];

//开始监听
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeClicked:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

//进入后台--不处理监听事件
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(intoBackg) name:UIApplicationDidBecomeActiveNotification object:nil];

//进入前台--处理监听事件
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(returnBackg) name:UIApplicationWillResignActiveNotification object:nil];

//隐藏系统的音量增减的视图
    MPVolumeView *volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
    volumeView.center = CGPointMake(-550,370);//设置中心点,让音量视图不显示在屏幕中

    [volumeView sizeToFit];
    [self.view addSubview:volumeView];
}

- (void)intoBackg{//回到前台才开始监听音量键
    _isIntoBg = NO;
}
- (void)returnBackg{//进入后台不监听音量键
    _isIntoBg = YES;
}

- (void)viewWillAppear:(BOOL)animated {//只在当前界面响应监听音量调节的事件
    [super viewWillAppear:YES];
    #ifdef DEBUG
        _isIntoBg = NO;
    #endif
}

- (void)viewWillDisappear:(BOOL)animated {//只在当前界面响应监听音量调节的事件
    [super viewWillDisappear:animated];
    #ifdef DEBUG
        //离开当前界面后就清空所有的数据
        [NSKeyedArchiver archiveRootObject:@[] toFile:[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"volumeKeyDEBUGData.data"]];
        _isIntoBg = YES;
    #endif
}

- (void)volumeClicked:(NSNotification *)noti{//处理音量键
    //获取音量键
    float volume =[[[noti userInfo]objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]floatValue];
    NSString *str1 = [[noti userInfo]objectForKey:@"AVSystemController_AudioCategoryNotificationParameter"];
    NSString *str2 = [[noti userInfo]objectForKey:@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"];
    if (([str1 isEqualToString:@"Audio/Video"] || [str1 isEqualToString:@"Ringtone"]) && ([str2 isEqualToString:@"ExplicitVolumeChange"])){
        if (volume > _currentVolume) {//预留,看需求是点击哪个打印结果,现在是音量加减都打印
                NSLog(@"点击的是音量加");
        }else{
                NSLog(@"点击的是音量减");
        }
        _currentVolume = volume;
        if ([SDGridItemCacheTool getRequestAndVolumeKeyDEBUG] && _isIntoBg == NO) {
                  BYErrorVC *errorVC = [[BYErrorVC alloc]init];
                  errorVC.msgArr = [SDGridItemCacheTool getRequestAndVolumeKeyDEBUG];
                [self.navigationController pushViewController:errorVC animated:YES];
                }
        }
}

- (void)dealloc{//移除音量键的监听
     #ifdef DEBUG
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
    #endif
}

你可能感兴趣的:(监听音量键--展示出当前界面的所有请求信息)