Search code examples
iosios7volumempmusicplayercontroller

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.


Solution

  • 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)