How to Change an iOS Device Volume Programmatically

How to change volume programmatically on iOS 11.4

Changing volumeViewSlider.value after a small delay resolves problem.

- (IBAction)increase:(id)sender {
MPVolumeView *volumeView = [[MPVolumeView alloc] init];
UISlider *volumeViewSlider = nil;

for (UIView *view in volumeView.subviews) {
if ([view isKindOfClass:[UISlider class]]) {
volumeViewSlider = (UISlider *)view;
break;
}
}

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
volumeViewSlider.value = 0.5f;
});
}

Swift version

How to change an iOS device volume programmatically?

I'm pretty sure that it is not possible to control the actual device volume (as this would also be a bit obtrusive) Controlling some media you're playing is another thing. You could however look into MPVolumeView: https://developer.apple.com/library/ios/documentation/MediaPlayer/Reference/MPVolumeView_Class/index.html for displaying a view for setting the volume.

The question has also been discussed here:
How to change device Volume on iOS - not music volume

Swift: how to set the iphone volume programmatically

You just need to import MediaPlayer. You can do as follow:

import MediaPlayer        

And to set the volume to maximum

(MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(1, animated: false)

iOS 9: How to change volume programmatically without showing system sound bar popup?

MPVolumeView has a slider, and by changing the value of the slider, you can change the device volume. I wrote an MPVolumeView extension to easily access the slider:

extension MPVolumeView {
var volumeSlider:UISlider {
self.showsRouteButton = false
self.showsVolumeSlider = false
self.hidden = true
var slider = UISlider()
for subview in self.subviews {
if subview.isKindOfClass(UISlider){
slider = subview as! UISlider
slider.continuous = false
(subview as! UISlider).value = AVAudioSession.sharedInstance().outputVolume
return slider
}
}
return slider
}
}

Change OS X system volume programmatically

You need to get the default audio device first:

#import <CoreAudio/CoreAudio.h>

AudioObjectPropertyAddress getDefaultOutputDevicePropertyAddress = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};

AudioDeviceID defaultOutputDeviceID;
UInt32 volumedataSize = sizeof(defaultOutputDeviceID);
OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject,
&getDefaultOutputDevicePropertyAddress,
0, NULL,
&volumedataSize, &defaultOutputDeviceID);

if(kAudioHardwareNoError != result)
{
// ... handle error ...
}

You can then set your volume on channel 1 (left) and channel 2 (right). Note that channel 0 (master) does not seem to be supported (the set command returns 'who?')

AudioObjectPropertyAddress volumePropertyAddress = {
kAudioDevicePropertyVolumeScalar,
kAudioDevicePropertyScopeOutput,
1 /*LEFT_CHANNEL*/
};

Float32 volume;
volumedataSize = sizeof(volume);

result = AudioObjectSetPropertyData(defaultOutputDeviceID,
&volumePropertyAddress,
0, NULL,
sizeof(volume), &volume);
if (result != kAudioHardwareNoError) {
// ... handle error ...
}

Hope this answers your question!



Related Topics



Leave a reply



Submit