倒带时AVPlayer currentTime错误

时间:2018-11-05 12:30:24

标签: swift avplayer

倒带音频文件时我有AVPlayer,所以当前时间长于文件总时长。谁知道问题出在哪里,为什么现在的时间不正确?

@objc func updateProgressBar(){
    guard let value = AppDelegate.avPlayer.currentItem?.currentTime().seconds else { return }
    let time = Func.getHoursMinutesSecondsFrom(seconds: value)
    DispatchQueue.main.async {
        self.startTime.text = time.fullTime
    }
}


func durationAudio(){
    // расчитывает время аудиозвука
    guard let duration = AppDelegate.avPlayer.currentItem?.asset.duration else { return }
    let time = Func.getHoursMinutesSecondsFrom(seconds: CMTimeGetSeconds(duration))
    DispatchQueue.main.async {
        self.endTime.text = time.fullTime
    }
}

转换为小时,分钟,秒

static func getHoursMinutesSecondsFrom(seconds: Double) -> (hours: Int, minutes: Int, seconds: Int, fullTime:String) {
    let secs = Int(seconds)
    let hours = secs / 3600
    let minutes = (secs % 3600) / 60
    let seconds = (secs % 3600) % 60
    let duration:String!
    if hours != 0 {
        duration = String(format:"%02d:%02d:%02d", hours, minutes, seconds)
    } else {
        duration = String(format:"%02d:%02d", minutes, seconds)
    }
    return (hours, minutes, seconds, duration)
}

倒带音频

func seekTo(completion:Bool){
    let duration = CMTimeGetSeconds(AppDelegate.avPlayer.currentItem!.asset.duration)
    let value = self.sliderSong.value
    let durationToSeek = Float(duration) * value
    let timeScale = AppDelegate.avPlayer.currentItem!.duration.timescale
    AppDelegate.avPlayer.seek(to: CMTimeMakeWithSeconds(Float64(durationToSeek), preferredTimescale: timeScale), toleranceBefore: CMTime.zero, toleranceAfter: CMTime.zero) { [weak self](bool) in
        guard completion else { return }
        self?.seeking = false
    }
}

0 个答案:

没有答案