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
How to Change Text Font in Uipickerview in iOS 7
Swift Unsafemutablepointer<Unmanaged<Cfstring>> Allocation and Print
Open Viewcontroller from Tab Like in Instagram Camera Page on iOS
iOS Ibeacon: How to Get All of Proximityuuid Programmatically
Implementing Autocomplete in iOS
How to Save Custom Objects in Array and Store It in Nsuserdefaults - Iphone
Xcode 9 Simulator Error Unable to Boot the Simulator Launchd Failed to Respond MAC Os Sierra
Mpmediaitemartwork Is Null While Cover Is Available in Itunes
Module 'Googlemobileads' Not Found in iOS
Swiftui Foreach Index Out of Range Error When Removing Row
Uploading Image with iOS App to Server File Size Is Too Large
Fbsdkaccesstoken Currentaccesstoken Is Not Being Updated After Log In
Custom Uitableviewcell with Progress Bar Download Update
How to Have Firebase Automatically Delete Values Older Than 30 Minutes