get ringer volume level using AVSystemController in ios

走远了吗. 提交于 2019-12-23 12:46:48

问题


How can i get iphone's current ringer volume level using AVSystemController

I also tried below code to check volume level

MPVolumeView *slide = [MPVolumeView new];
UISlider *volumeViewSlider;

for (UIView *view in [slide subviews]){
    if ([[[view class] description] isEqualToString:@"MPVolumeSlider"]) {
        volumeViewSlider = (UISlider *) view;
    }
}
float val = [volumeViewSlider value];

But when i print / check val, it returns 1.00

I also tried below code

musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
Nslog(@"%f",musicPlayer.volume);

But it also returns 1.00 event if my phone is silent. I refer below link How to determine the current level of the iPhone ringer? but i could not find solution.

Please help.


回答1:


Try this:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(volumeChanged:)
                                             name:@"AVSystemController_SystemVolumeDidChangeNotification"
                                           object:nil];

- (void)volumeChanged:(NSNotification *)notification{
   NSDictionary*dict=notification.userInfo;
   float newVolume =
   [[dict objectForKey:@"AVSystemController_AudioVolumeNotificationParameter"]floatValue];
    //my comment: newVolume is value that you need
}


来源:https://stackoverflow.com/questions/23517085/get-ringer-volume-level-using-avsystemcontroller-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!