如何限制按钮的音量(Swift)
我有代码为按钮添加声音。
我怎样才能这样做,无论设备的声音状态如何,键的音量总是在同一水平?
var audioPlayer = AVAudioPlayer()
@IBAction func Sound(_ sender: UIButton) {
guard UserDefaults.standard.bool(forKey: "SwitchState2") else { return }
audioPlayer.play()
}
override func viewDidLoad() {
super.viewDidLoad()
let music = Bundle.main.path(forResource: "PBOING1", ofType: "WAV")
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: music! ))
}
catch{
print(error)
}
}
答案 0 :(得分:2)
尝试设置音量:audioplayer.volume = 0.0 to 1.0
您可以在此处详细了解AVAudioPlayer
:
https://developer.apple.com/documentation/avfoundation/avaudioplayer/1389330-volume