ios 获取系统音量以及监听系统音量的设置

获取设备的音量:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];

 CGFloat   _systemVoluemValue = audioSession.outputVolume;

 

增加和删除监听设备音量的代码

添加设备音量的监听

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onVolumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];

        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

 

删除设备音量的监听:

  [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];

 

-(void)onVolumeChanged:(NSNotification *)notification{

    NSLog(@"----notification---%@",notification);

    if ([[notification.userInfo objectForKey:@"AVSystemController_AudioCategoryNotificationParameter"] isEqualToString:@"Audio/Video"]) {

        if ([[notification.userInfo objectForKey:@"AVSystemController_AudioVolumeChangeReasonNotificationParameter"] isEqualToString:@"ExplicitVolumeChange"]) {

            CGFloat volume = [[notification.userInfo objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"] floatValue];

           //根据音量处理相关的设置

        }

    }

}

你可能感兴趣的:(ios,移动开发)