AVAudioPlayer 当前时间超过持续时间

发布于 2024-12-04 11:47:09 字数 561 浏览 6 评论 0原文

我正在使用 AVAudioPlayer 制作自己的音频播放器。

注意:“p”是我的播放器实例

以下是我在标签之一中读取曲目进度的方式:

currentTime.text = [NSString stringWithFormat:@"%d:%02d", (int)p.currentTime / 60, (int)p.currentTime % 60];

以下是我在标签之一中设置曲目总持续时间的方式:

int seconds = (int)p.duration % 60;
    int minutes = (int)p.duration / 60;

    duration.text = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];

当我在设备上运行应用程序时,曲目的当前时间始终超过持续时间(大约 5-10 秒)。

这是 AVAudioPlayer 中的错误,还是我做得不正确?

注意:此行为也会发生在设备上(不仅仅是模拟器上)

I'm making my own audio player using AVAudioPlayer.

NOTE: "p" is my instance of the player

Here's how I'm reading the track progress in one of the labels:

currentTime.text = [NSString stringWithFormat:@"%d:%02d", (int)p.currentTime / 60, (int)p.currentTime % 60];

Here's how I set the total duration of the track in one of the labels:

int seconds = (int)p.duration % 60;
    int minutes = (int)p.duration / 60;

    duration.text = [NSString stringWithFormat:@"%d:%02d", minutes, seconds];

When I run the app on the device, the track's current time ALWAYS exceeds the duration (by about 5-10 seconds).

Is this a bug in AVAudioPlayer, or am I not doing it correctly?

NOTE: This behavior also occurs on the device (not just on the simulator)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单挑你×的.吻 2024-12-11 11:47:09

使用 % 60 求出秒数后,在转换剩余的分钟数时应删除这些秒数。例如,在找到 59 秒后,总持续时间为 119 秒,您应该将其从 119 中删除,然后进行 60 秒的分钟转换 (119-59)。这可能会解决你的问题。

After finding the seconds by using % 60, you should remove those seconds when converting the remaining for the minutes. For e.g., with the total duration of 119 seconds after finding 59 seconds you should remove that from 119 and then do minute conversion for 60 seconds (119-59). That might solve your problem.

浪菊怪哟 2024-12-11 11:47:09

分钟应该是浮动的:152 秒 / 60.0f = 2.5333 而不是 2
话虽这么说,如果您想显示剩余分钟而不您已经获得的秒数: int 分钟 = (p.duration-seconds) / 60

另外,为了更好按照您想要的方式格式化时间的方法,请查看 这个问题(不是公认的解决方案)。

Minutes should be float: 152 seconds / 60.0f = 2.5333 not 2.
That being said, if you want to show the remaining minutes without the seconds you already obtain: int minutes = (p.duration-seconds) / 60

Also, for a better method to format time the way you want to, have a look at the second answer in this question (not the accepted solution).

纸伞微斜 2024-12-11 11:47:09

这是函数:

func setTimeString(duration: Double)->String {

    var audioDurationSeconds = duration
    var expression = ""
    var minutesString = "00"
    var minutesFloat = 0.0

    if (audioDurationSeconds)/60 >= 1 {

        minutesFloat = (audioDurationSeconds) / 60
        audioDurationSeconds = TimeInterval(Int(audioDurationSeconds)%60)

        if minutesFloat < 10.0 {
            minutesString = String.init(format: "0%.f", floor(minutesFloat))

        } else {
            minutesString = String.init(format: "%.f", floor(minutesFloat))
        }
    }

    if audioDurationSeconds < 10.0 {
        expression = String.init(format: "%@:0%i", minutesString, Int(audioDurationSeconds))
    } else {
        expression = String.init(format: "%@:%i", minutesString, (Int(audioDurationSeconds)))
    }

    return expression
}

Here is the function:

func setTimeString(duration: Double)->String {

    var audioDurationSeconds = duration
    var expression = ""
    var minutesString = "00"
    var minutesFloat = 0.0

    if (audioDurationSeconds)/60 >= 1 {

        minutesFloat = (audioDurationSeconds) / 60
        audioDurationSeconds = TimeInterval(Int(audioDurationSeconds)%60)

        if minutesFloat < 10.0 {
            minutesString = String.init(format: "0%.f", floor(minutesFloat))

        } else {
            minutesString = String.init(format: "%.f", floor(minutesFloat))
        }
    }

    if audioDurationSeconds < 10.0 {
        expression = String.init(format: "%@:0%i", minutesString, Int(audioDurationSeconds))
    } else {
        expression = String.init(format: "%@:%i", minutesString, (Int(audioDurationSeconds)))
    }

    return expression
}
粉红×色少女 2024-12-11 11:47:09
extension UILabel{

    func getTimeString(from duration:Double) -> String{

        let hours   = Int(duration / 3600)
        let minutes = Int(duration / 60) % 60

        let seconds = Int(duration.truncatingRemainder(dividingBy: 60))
        if hours > 0 {
            return String(format: "%i:%02i:%02i", arguments: [hours,minutes,seconds])
        }else {
            return String(format: "%02i:%02i", arguments: [minutes,seconds])
        }

    }
extension UILabel{

    func getTimeString(from duration:Double) -> String{

        let hours   = Int(duration / 3600)
        let minutes = Int(duration / 60) % 60

        let seconds = Int(duration.truncatingRemainder(dividingBy: 60))
        if hours > 0 {
            return String(format: "%i:%02i:%02i", arguments: [hours,minutes,seconds])
        }else {
            return String(format: "%02i:%02i", arguments: [minutes,seconds])
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文