gpt4 book ai didi

swift - 根据音频播放器当前时间更新 UILabel

转载 作者:行者123 更新时间:2023-11-30 12:25:41 26 4
gpt4 key购买 nike

我正在尝试根据 AudioPlayer 当前时间的持续时间更新 UILabel。我不想使用计时器,因为在我的应用程序中,人们会用很短的持续时间来记录自己,而且半秒很重要,所以我真的不想使用计时器()。相反,我试图做的只是根据当前时间自动更新标签。但我似乎无法弄清楚......

这是我的做法,但显然我做得不正确

var audioPlayer : AVAudioPlayer!

var audioTime = UILabel() {
didSet {
do {
try audioPlayer = AVAudioPlayer(contentsOf: audioURL!)
let currentTime = Int(audioPlayer.currentTime)
let minutes = currentTime/60
let seconds = currentTime - minutes * 60
audioTime.text = (NSString(format: "%02d:%02d", minutes,seconds) as String)

} catch {

}
}
}

最佳答案

这可能对你有帮助!将 Outlet 连接到 showTimeLbl 并尝试。

class ViewController: UIViewController {

@IBOutlet weak var showTimeLbl: UILabel!
var timeCount:Int = 0
var timer:Timer!

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if(timer != nil)
{
timer.invalidate()
}
timer = Timer(timeInterval: 1.0, target: self, selector: #selector(ViewController.timerDidFire), userInfo: nil, repeats: true)
RunLoop.current.add(timer, forMode: RunLoopMode.commonModes)

}

func timerDidFire()
{
timeCount += 1

showTimeLbl.text = NSString(format: "%02d:%02d:%02d", timeCount/3600,(timeCount/60)%60,timeCount%60) as String

}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
timer?.invalidate()
timer = nil
}

关于swift - 根据音频播放器当前时间更新 UILabel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44251484/

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