Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 7: MPMusicPlayerController volume deprecated. How to change device volume now?

MPMusicPlayerController setVolume is deprecated since iOS 7

Is there any other way to change system music volume? Preferably without user interaction. Its important feature: to increase volume automatically for any alarm clock from AppStore.

like image 884
Maxim Kholyavkin Avatar asked Oct 07 '13 06:10

Maxim Kholyavkin


2 Answers

To answer you question exactly: Yes there is other way to change system volume without user interaction.

Until recent times I used to think that changing volume using MPVolumeView programmatically is possible only using private API. But I have just verified, that changing the value of volumeSlider and faking slider's touchUP event works:

MPVolumeView* volumeView = [[MPVolumeView alloc] init];  //find the volumeSlider UISlider* volumeViewSlider = nil; for (UIView *view in [volumeView subviews]){     if ([view.class.description isEqualToString:@"MPVolumeSlider"]){         volumeViewSlider = (UISlider*)view;         break;     } }  [volumeViewSlider setValue:1.0f animated:YES]; [volumeViewSlider sendActionsForControlEvents:UIControlEventTouchUpInside]; 

(When slider receives touchUP event, it will invoke _commitVolumeChange method on itself, which will change the system volume)

like image 174
ambientlight Avatar answered Sep 20 '22 10:09

ambientlight


Until Apple sees fit to rescind this decision there are two remedies I have discovered:

  • Keep using the volume property, it is still working under iOS 7.0.2
  • Use AVAudioSession.outputVolume to read the volume when your app wakes and pop up an alert containing an MPVolumeView if the volume is lower than (or higher than) a user specified value. At least your user knows that their alarm (or whatever) will play quietly and has the opportunity to adjust the volume. Alternately you could just display very clearly the volume level so they get no surprises.
like image 40
amergin Avatar answered Sep 21 '22 10:09

amergin