2014-03-03 116 views
5

我正在創建一個應用程序,我想要檢測到iPhone處於靜音模式或不。如何使iPhone進入靜默模式開啓/關閉使用目標c

我也早已想到了下面的鏈接

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

Switching between silent mode and ring mode on an iPhone

很多人說,這是對抗iPhone的政策。但我見過很多應用程序,它也給這個功能

檢查下面的應用鏈接

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

我也想爲上述應用做改變iPhone的靜音模式相同。

有人知道嗎?

+1

您是否找到答案?我也在尋找同樣的東西 – rat

回答

0

有辦法,但你的申請將被蘋果拒絕(我不確定,可能不會)。將MPVolumeView添加到視圖中,但不要將其顯示給用戶(爲此,您可以根據視圖更改框架)。您可以從此控件獲取聲音級別。這裏是代碼(測試iOS + 7):


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