Ok,
This gets the system volume, works well.
var volume = AVAudioSession.sharedInstance().outputVolume
print("output volume: \(volume)")
But how do I set the system volume [in Swift] please
When you're on the phone or listening to songs, movies, or other media on iPhone, you can use the buttons on the side of your device to adjust the audio volume. Otherwise, the buttons control the volume for the ringer, alerts, and other sound effects. You can also use Siri to turn the volume up or down.
There is nothing that allows you to control the volume of specific apps in the stock OS. In some apps there could be an option in Settings to manually adjust the volume but that depends if the app developer implements it. This way it will adjust the volume of that specific app.
On your device, open Settings > Music > Volume Limit and you'll see a slider that represents the maximum volume, which at the moment will most likely be completely to the right.
Here is what you need to do with latest iOS 10.2
Step 1. Import MediaPlayer
import MediaPlayer
Step 2. Add extension
//Update system volume
extension MPVolumeView {
static func setVolume(_ volume: Float) {
let volumeView = MPVolumeView()
let slider = volumeView.subviews.first(where: { $0 is UISlider }) as? UISlider
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.01) {
slider?.value = volume
}
}
}
Step 3. Change volume this way
//Update system volume
MPVolumeView.setVolume(0.2)
Thanks to trungduc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With