gpt4 book ai didi

ios - 在 XCODE 8 Swift 3 中为倒数计时器添加音调

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:14:33 25 4
gpt4 key购买 nike

我是编码新手。我正在尝试学习 iOS 开发,我创建了一个简单的倒数计时器,它使用 slider 来选择以秒为单位的时间量。我想为倒计时的最后 9 秒添加一个基调。我知道如何添加音频文件,但我“不知道”如何为计时器的最后 9 秒添加倒计时音。

@IBAction func startBtnTapped(_ sender: Any) {
timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.counter), userInfo: nil, repeats: true)

SoundPlayer4.play()

//Hide Start and Slider Buttons
startBtn.isHidden = true
sliderCtrl.isHidden = true
stopBtn.isHidden = false

}

@IBAction func stopBtnTapped(_ sender: Any) {
timer.invalidate()
seconds = 180
sliderCtrl.setValue(180, animated: true)
timerLbl.text = "180"

SoundPlayer3.play()

//Show Start and Slider Buttons
sliderCtrl.isHidden = false
startBtn.isHidden = false
stopBtn.isHidden = true
}

@IBAction func sliderCrtlUsed(_ sender: UISlider) {
seconds = Int(sender.value)
timerLbl.text = String(seconds)
}

func counter(){
seconds -= 1
timerLbl.text = String(seconds)

if (seconds == 0){
timer.invalidate()

//Show Start and Slider Buttons
startBtn.isHidden = false
sliderCtrl.isHidden = false
stopBtn.isHidden = true

//Play Audio
SoundPlayer1.play()
}
}

那么谁能帮我在我的项目中添加倒计时音调呢?谢谢

最佳答案

将此代码放入counter 函数中:

if seconds < 10 {

var AudioURL = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Dog bark", ofType: "mp3")!)
var AudioPlayer = AVAudioPlayer()

do {
AudioPlayer = try AVAudioPlayer(contentsOfURL: AudioURL)
} catch _ as NSError {
fatalError()
}

AudioPlayer.play()
}

然后把文件放在这里:

enter image description here

就是这样!

You can read more on this question here.

关于ios - 在 XCODE 8 Swift 3 中为倒数计时器添加音调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42216562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com