gpt4 book ai didi

swift - AVPlayer 跳过特定时间

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

我正在寻找解决方案来制作一个功能,例如每次用户点击按钮时将 AVPlayer 中的视频转发 10%(10% + 下一次点击另外 10% + 下一次点击另外 10%等)从视频的当前状态。到目前为止,我已经找到了一个 seek(to:CMTimeMakeWithSeconds(seconds: Float64, preferredTimescale: Int32) ,但是它每次都会将视频移动到特定时间,而不是特定时间。

有什么建议吗?

最佳答案

你只需要做几个简单的数学步骤,在 Swift 3 中它应该像这样:

private func skipBy(percentage: Float64) {

guard let durationTime = player.currentItem?.duration else { return }

// Percentage of duration
let percentageTime = CMTimeMultiplyByFloat64(durationTime, percentage)

guard percentageTime.isValid && percentageTime.isNumeric else { return }

// Percentage plust current time
var targetTime = player.currentTime() + percentageTime
targetTime = targetTime.convertScale(durationTime.timescale, method: .default)

// Sanity checks
guard targetTime.isValid && targetTime.isNumeric else { return }

if targetTime > durationTime {
targetTime = durationTime // seek to end
}

player.seek(to: targetTime)
}

有关 AVPlayer 的一个很好的例子,请参阅开源、社区开发、非官方 WWDC app .

关于swift - AVPlayer 跳过特定时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45131538/

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