你只需要做一些簡單的數學運算步驟,斯威夫特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。