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

我只是一个虾纸丫 提交于 2019-12-18 04:12:44

问题


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


回答1:


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)


来源:https://stackoverflow.com/questions/36322856/swift-how-to-set-the-iphone-volume-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!