我正在创建一个应用程序,我想在其中检测到iphone是否处于静音模式。
我已经想到了下面的链接
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的静音模式,就像上面的应用程序一样。
有人知道吗?
答案 0 :(得分:0)
有办法,但你的申请将被Apple拒绝(我不确定,可能不是)。将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];
}