Swift:如何以编程方式设置iphone音量 [英] Swift: how to set the iphone volume programmatically
本文介绍了Swift:如何以编程方式设置iphone音量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个闹钟应用程序,我注意到Sleep Cycle之类的应用程序会在闹钟响起之前将音量设置为最大,即使音量非常低。
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.
我尝试过使用AVAudioPlayer,我在MPVolumeView上看到的只是用户必须这样做。
我有什么办法可以在没有用户的情况下将其打开吗?
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?
感谢您的帮助。
如果我能在swift中获取代码
If I could please get the code in swift
推荐答案
您只需要导入MediaPlayer。你可以这样做:
You just need to import MediaPlayer. You can do as follow:
import MediaPlayer
并将音量设置为最大值
(MPVolumeView().subviews.filter{NSStringFromClass($0.classForCoder) == "MPVolumeSlider"}.first as? UISlider)?.setValue(1, animated: false)
这篇关于Swift:如何以编程方式设置iphone音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文