失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > iOS 设置系统音量和监听系统音量变化

iOS 设置系统音量和监听系统音量变化

时间:2018-08-15 14:48:57

相关推荐

iOS 设置系统音量和监听系统音量变化

很简单的调用

首先在工程引入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];}

如果觉得《iOS 设置系统音量和监听系统音量变化》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。