很簡單的調用?
首先在工程引入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];}
?