How to change device Volume on iOS - not music volume

To answer brush51's question:

How can i do that? just change the DEVICE volume?

As 0x7fffffff suggested:

You cannot change device volume programatically, however MPVolumeView (volume slider) is there to change device volume but only through user interaction.

So, Apple recommends using MPVolumeView, so I came up with this:

Add volumeSlider property:

@property (nonatomic, strong) UISlider *volumeSlider;

Init MPVolumeView and add somewhere to your view (can be hidden, without frame, or empty because of showsRouteButton = NO and showsVolumeSlider = NO):

MPVolumeView *volumeView = [MPVolumeView new];
volumeView.showsRouteButton = NO;
volumeView.showsVolumeSlider = NO;
[self.view addSubview:volumeView];

Find and save reference to UISlider:

__weak __typeof(self)weakSelf = self;
[[volumeView subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if ([obj isKindOfClass:[UISlider class]]) {
        __strong __typeof(weakSelf)strongSelf = weakSelf;
        strongSelf.volumeSlider = obj;
        *stop = YES;
    }
}];

Add target action for UIControlEventValueChanged:

[self.volumeSlider addTarget:self action:@selector(handleVolumeChanged:) forControlEvents:UIControlEventValueChanged];

And then detect volume changing (i.e. by the hardware volume controls):

- (void)handleVolumeChanged:(id)sender
{
    NSLog(@"%s - %f", __PRETTY_FUNCTION__, self.volumeSlider.value);
}

and also other way around, you can set volume by:

self.volumeSlider.value = < some value between 0.0f and 1.0f >;

Hope this helps (and that Apple doesn't remove MPVolumeSlider from MPVolumeView).


Here's what I've done:

func setSystemVolume(volume: Float) {
    let volumeView = MPVolumeView()

    for view in volumeView.subviews {
        if (NSStringFromClass(view.classForCoder) == "MPVolumeSlider") {
            let slider = view as! UISlider
            slider.setValue(volume, animated: false)
        }
    }
}

As property volume in MPMusicPlayerController was deprecate in iOS 7. This is only method you can do that.


This (Swift) solution worked great for me: http://weimenglee.blogspot.com/2015/07/ios-tip-programmatically-adjust-device.html

import MediaPlayer

let volumeView = MPVolumeView()
if let view = volumeView.subviews.first as? UISlider{
    view.value = 0.1 //---0 t0 1.0---

}

You have to use applicationMusicPlayer instead of iPodMusicPlayer to set the system volume:

#import <MediaPlayer/MediaPlayer.h>
musicPlayer = [MPMusicPlayerController applicationMusicPlayer];
musicPlayer.volume = 1; // max volume
musicPlayer.volume = 0; // min volume (mute)
musicPlayer.volume = 0.0625; // 1 bar on the overlay volume display