简体   繁体   English

Swift - 通过UISlider进行AVPlayer

[英]Swift - AVPlayer progress via UISlider

I am trying to make video player where I need to show the progress via UISlider and UILabel (for updating time). 我正在尝试制作视频播放器,我需要通过UISliderUILabel显示进度(用于更新时间)。 Here is my code 这是我的代码

let videoPlayer = AVPlayer()
var videoPlayerSlider: UISlider = UISlider()
var videoPlayerLabel: UILabel = UILabel()

    func updateVideoPlayerSlider() {
        guard let currentTime = videoPlayer.currentTime else {
            return
        }
        let mins = currentTime / 60
        let secs = currentTime.truncatingRemainder(dividingBy: 60)
        let timeformatter = NumberFormatter()
        timeformatter.minimumIntegerDigits = 2
        timeformatter.minimumFractionDigits = 0
        timeformatter.roundingMode = .down
        guard let minsStr = timeformatter.string(from: NSNumber(value: mins)), let secsStr = timeformatter.string(from: NSNumber(value: secs)) else {
            return
        }
        videoPlayerLabel.text = "\(minsStr).\(secsStr)"
        videoPlayerSlider.value = Float(videoPlayer.currentTime())
    }

It shows 2 error. 它显示2错误。

1.(at very 1st line of the function)Initializer for conditional binding must have optional type, not '() -> CMTime 1.(在函数的第1行)条件绑定的初始化程序必须具有可选类型,而不是'() - > CMTime

2.(at last line of the function)Cannot invoke initializer for type 'Float' with an argument list of type '(CMTime)' 2.(在函数的最后一行)无法使用类型为“(CMTime)”的参数列表调用类型为“Float”的初始值设定项

Any assistance would be appreciated. 任何援助将不胜感激。

let videoPlayer = AVPlayer()
var videoPlayerSlider: UISlider = UISlider()
var videoPlayerLabel: UILabel = UILabel()

func updateVideoPlayerSlider() {
    // 1 . Guard got compile error because `videoPlayer.currentTime()` not returning an optional. So no just remove that.
    let currentTimeInSeconds = CMTimeGetSeconds(videoPlayer.currentTime())
    // 2 Alternatively, you could able to get current time from `currentItem` - videoPlayer.currentItem.duration

    let mins = currentTimeInSeconds / 60
    let secs = currentTimeInSeconds.truncatingRemainder(dividingBy: 60)
    let timeformatter = NumberFormatter()
    timeformatter.minimumIntegerDigits = 2
    timeformatter.minimumFractionDigits = 0
    timeformatter.roundingMode = .down
    guard let minsStr = timeformatter.string(from: NSNumber(value: mins)), let secsStr = timeformatter.string(from: NSNumber(value: secs)) else {
        return
    }
    videoPlayerLabel.text = "\(minsStr).\(secsStr)"
    videoPlayerSlider.value = Float(currentTimeInSeconds) // I don't think this is correct to show current progress, however, this update will fix the compile error

    // 3 My suggestion is probably to show current progress properly
    if let currentItem = videoPlayer.currentItem {
        let duration = currentItem.duration
        if (CMTIME_IS_INVALID(duration)) {
            // Do sth
            return;
        }
        let currentTime = currentItem.currentTime()
        videoPlayerSlider.value = Float(CMTimeGetSeconds(currentTime) / CMTimeGetSeconds(duration))
    }
}

I hope this would help you 我希望这会对你有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM