簡體   English   中英

AVPlayer跳過特定時間

[英]AVPlayer skip by a specific time

我正在尋找實現此功能的解決方案,例如,每當用戶單擊按鈕時,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應用程序

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM