gpt4 book ai didi

swift - 如何在播放时获取标签中 AVAudioplayer 的当前时间

转载 作者:行者123 更新时间:2023-12-05 09:18:52 30 4
gpt4 key购买 nike

我尝试构建一个应用来播放 MP3 文件。我想获取当前时间和持续时间,以便在播放时将其显示在 2 个标签中。

我使用了这段代码:

let duration = Int((player1?.duration)!)
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
durLabel.text = NSString(format: "%02d:%02d", minutes2,seconds2) as String


let currentTime1 = Int((player1?.currentTime)!)
let minutes = currentTime1/60
let seconds = currentTime1 - minutes * 60
curLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as String

在持续时间中,它显示歌曲的一半时间。

例如:如果歌曲的持续时间是 20 分 40 秒,则显示 10:20 的一半。但是在当前时间的情况下没有进展,显示00:00

非常感谢。

最佳答案

    // this is to compute and show remaining time
let duration = Int((player1?.duration - (player1?.currentTime))!)
let minutes2 = duration/60
let seconds2 = duration - minutes2 * 60
durLabel.text = NSString(format: "%02d:%02d", minutes2,seconds2) as String

//This is to show and compute current time
let currentTime1 = Int((player1?.currentTime)!)
let minutes = currentTime1/60
let seconds = currentTime1 - minutes * 60
curLabel.text = NSString(format: "%02d:%02d", minutes,seconds) as String

关于swift - 如何在播放时获取标签中 AVAudioplayer 的当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43105964/

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