Swift: how to set iphone volume programmatically

I'm trying to create an application for an alarm, and I noticed that applications such as Sleep Cycle will set your volume to the maximum before the alarm goes off, even if you have a too low volume level.

I tried using AVAudioPlayer, and all I saw with MPVolumeView is that the user has to do this. Is there a way I can turn it off if the user should not do this?

Thank you for your help.

If I could quickly get the code

+5
source share
1 answer

You just need to import MediaPlayer. You can do the following:

import MediaPlayer 

And to set the volume to maximum

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

Source: https://habr.com/ru/post/1246113/


All Articles