Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting the system volume in Swift under IOS

Tags:

ios

swift

audio

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

like image 386
user3069232 Avatar asked Jun 17 '16 05:06

user3069232


People also ask

How do I adjust media volume on IOS 15?

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.

Can you adjust iPhone app volume?

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.

Is there a master volume on iPhone?

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.


1 Answers

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

like image 125
swiftBoy Avatar answered Sep 28 '22 08:09

swiftBoy