獲取設備的音量:
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];
//根據音量處理相關的設置
}
}
}