IOS 自定义音量改变UI

首先是系统自己的音量改变UI


啦啦啦

然后产品经理说了,看视频的时候很不爽,我要自己定制这个东西,系统的不去显示。

然后是修改之后的UI



IOS 自定义音量改变UI_第1张图片
请看最上边

搜了一下需要用到MPVolumeView 

具体做法:

1、首先监听系统音量按钮点击的事件

[[NSNotificationCenterdefaultCenter]addObserver:self selector:@selector(volumeChanged:) name:AVSystemController_SystemVolumeDidChangeNotificationobject:nil]; 

这个时候点击了音量加减按钮在volumeChanged方法中就可以收到回调

2、监听收到了就该实现产品经理的需求了,把系统自己的音量UI去掉

搜了一下说是MPVolumeView在当前VC的View层中的话就不会显示,但是实测的话还是会显示的。

后面找到方法:[[AVAudioSession sharedInstance]setActive:YES error:nil]; 设置为YES 并且MPVolumeView在当前View中就不会显示系统自己的音量调节的UI MPVolumeView的实例Viewhidden设为NO alpha不为0 宽高大概要大于1吧 没有实际测试 基本我自己设置的是CGRectMake(-1000,10,100,100) 只要不在屏幕上显示就行

3、- (void)volumeChanged:(NSNotification*)notification

notification的userInfo有两种 一种是音量的变化 一种是铃音的变化

/**

改变铃声的通知

"AVSystemController_AudioCategoryNotificationParameter" = Ringtone;    //铃声改变

"AVSystemController_AudioVolumeChangeReasonNotificationParameter" = ExplicitVolumeChange; //改变原因

"AVSystemController_AudioVolumeNotificationParameter" = "0.0625";  //当前值

"AVSystemController_UserVolumeAboveEUVolumeLimitNotificationParameter" = 0;最小值

改变音量的通知

"AVSystemController_AudioCategoryNotificationParameter" = "Audio/Video"; //音量改变

"AVSystemController_AudioVolumeChangeReasonNotificationParameter" = ExplicitVolumeChange; //改变原因

"AVSystemController_AudioVolumeNotificationParameter" = "0.3";  //当前值

"AVSystemController_UserVolumeAboveEUVolumeLimitNotificationParameter" = 0;最小值

*/

@end

你可能感兴趣的:(IOS 自定义音量改变UI)