Get call volume level using AVSystemController in ios

How can I get ringer volume on iphone using AVSystemController

I also tried under the code to check the 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 under the code

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

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

Please, help.

+4
source share
1 answer

Try the following:

[[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
}
+3
source

Source: https://habr.com/ru/post/1539565/


All Articles