我正在構建一個簡單的計數器,每次按下按鈕時都會發出聲音。 但是,我使用開關將聲音靜音的解決方案無效。有沒有什麼辦法來靜音AVAudioPlayer?停止似乎沒有幫助,我想這很正常,因爲沒有連續播放音樂。 這裏是我的代碼:Swift:使用開關關閉按鈕聲音
override func viewDidLoad() {
super.viewDidLoad()
}
var rowValue: Int = 0
let motivationalQuotes: [String] = ["xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", "xxx", ]
@IBOutlet weak var motivationLabel: UILabel!
@IBAction func subtractRowButton(sender: AnyObject) {
if rowValue > 0 {
rowValue = rowValue - 1
} else {
rowValue = 0
}
rowCount.text = "\(rowValue)"
playSubtractSound()
motivateThem()
}
@IBAction func addRowButton(sender: AnyObject) {
rowValue = rowValue + 1
rowCount.text = "\(rowValue)"
playAddSound()
motivateThem()
}
@IBOutlet weak var rowCount: UILabel!
@IBAction func resetButton(sender: AnyObject) {
rowValue = 0
rowCount.text = "\(rowValue)"
}
@IBOutlet weak var soundSwitch: UISwitch!
@IBAction func switchPressed(sender: UISwitch) {
if soundSwitch.on {
beepPlayer.volume = 0
} else {
beepPlayer.stop()
}
}
func motivateThem(){
if(rowValue > 0 && rowValue < 2)
{
motivationLabel.text = motivationalQuotes[0]
}
else if(rowValue > 19)
{
motivationLabel.text = motivationalQuotes[8]
}
}
let addSoundURL = NSBundle.mainBundle().URLForResource("add", withExtension: "aif")!
let subtractSoundURL = NSBundle.mainBundle().URLForResource("subtract", withExtension: "aif")!
var beepPlayer = AVAudioPlayer()
func playAddSound(){
beepPlayer = AVAudioPlayer(contentsOfURL: addSoundURL, error: nil)
beepPlayer.prepareToPlay()
beepPlayer.play()
}
func playSubtractSound(){
beepPlayer = AVAudioPlayer(contentsOfURL: subtractSoundURL, error: nil)
beepPlayer.prepareToPlay()
beepPlayer.play()
}
如何將volume屬性設置爲0來靜音? – rshankar