Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift: how to set the iphone volume programmatically [duplicate]

I am trying to create an alarm app and I have noticed that apps like Sleep Cycle will set your volume to max right before the alarm goes off, even if you had the volume at very low.

I have tried using AVAudioPlayer and all I have seen with MPVolumeView is that the user must do it. Is there any way I can turn it up without the user having to do it?

Thank you for your help.

If I could please get the code in swift

like image 947
Charles Avatar asked Mar 31 '16 02:03

Charles


1 Answers

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)
like image 68
Leo Dabus Avatar answered Sep 19 '22 21:09

Leo Dabus