How Make iPhone to silent mode on/off using objective c

蓝咒 提交于 2020-01-03 15:11:52

问题


I am creating one app in which i want to detect that iphone is on silent mode or not.

I have already gone thought the below link

Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

Switching between silent mode and ring mode on an iPhone

many people says that it is against the iPhone policy. but i have seen many app which is also giving this functionality

check the below app link

https://itunes.apple.com/us/app/silentalert/id506092189?mt=8

I also want to change the silent mode of the iPhone same as the above app is doing.

Is anyone know about that ?


回答1:


There is way but your application will be rejected by Apple (I am not sure, may be not). Add a MPVolumeView to your view, but do not show it to the user (for this purpose you can change the frame according to your view). You can get the sound level from this control. Here is the code (iOS +7 is tested):


    - (void)someMethod
    {
        MPVolumeView *systemVolumeSlider = [[MPVolumeView alloc] initWithFrame:  CGRectMake(-100, -100, 16, 16)];
        [systemVolumeSlider setUserInteractionEnabled:NO];
        systemVolumeSlider.showsRouteButton = NO;
        [self.view addSubview: systemVolumeSlider];
        [systemVolumeSlider sendSubviewToBack:self.view];

        [[AVAudioSession sharedInstance] setActive:YES error:NULL];

        float currentSoundLevel = [self getVolumeLevel];
        NSLog(@"volume level : %f", currentSoundLevel); // if it is 0, the phone is in silent mode

        // do your job here... 

        [[MPMusicPlayerController applicationMusicPlayer] setVolume:1.0]; // 1.0 is the max level

        // do your job here...
     }

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


来源:https://stackoverflow.com/questions/22142580/how-make-iphone-to-silent-mode-on-off-using-objective-c

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