有辦法,但你的申請將被蘋果拒絕(我不確定,可能不會)。將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];
}
您是否找到答案?我也在尋找同樣的東西 – rat