【问题标题】:AVPlayer skip by a specific timeAVPlayer 跳过特定时间
【发布时间】:2017-07-16 17:33:16
【问题描述】:

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

有什么建议吗?

【问题讨论】:

    标签: swift button video avplayer seek


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-11
      • 2023-03-03
      相关资源
      最近更新 更多