我是编码新手。我正在尝试学习 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()
}
然后把文件放在这里:
就是这样!
You can read more on this question here.
关于ios - 在 XCODE 8 Swift 3 中为倒数计时器添加音调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42216562/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |