你只需要做几个简单的数学步骤,在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