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

}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM