很简单的调用
首先在工程引入MediaPlayer.framework
#import <MediaPlayer/MediaPlayer.h>
1. 获取系统音量
// 获取系统音量
MPVolumeView *volumeView = [[MPVolumeView alloc] init]; UISlider *volumeViewSlider= nil; for (UIView *view in [volumeView subviews]){
if ([view.class.description isEqualToString:@"MPVolumeSlider"]){
volumeViewSlider = (UISlider*)view;
break;
}
} float systemVolume = volumeViewSlider.value;
2.监听方法
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(volumeChanged:) name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil];
- (void)volumeChanged:(NSNotification *)notification
{
[notification.userInfo[@"AVSystemController_AudioVolumeNotificationParameter"] floatValue]; }
3.记得销毁哦
- (void)dealloc
{ [[NSNotificationCenter defaultCenter] removeObserver:self name:@"AVSystemController_SystemVolumeDidChangeNotification" object:nil]; }