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

前端 未结 1 1396
傲寒
傲寒 2021-01-18 07:38

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

相关标签:
1条回答
  • 2021-01-18 07:55

    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];
        }
    
    0 讨论(0)
提交回复
热议问题