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

           //根據音量處理相關的設置

        }

    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章