获取设备的音量:

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];

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

        }

    }

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐