在ios中使用AVSystemController获取铃声音量

时间:2014-05-07 11:52:44

标签: ios volume avsystemcontroller

如何使用AVSystemController获取iphone当前的铃声音量?

我也试过下面的代码来检查音量

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

但是当我打印/检查val时,它返回1.00

我也试过下面的代码

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

但如果手机静音,它也会返回1.00事件。我在下面链接 How to determine the current level of the iPhone ringer?但我无法找到解决方案。

请帮忙。

1 个答案:

答案 0 :(得分:3)

试试这个:

[[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
}