如何在Swift 4中设置视频的偏移量?寻求(to :)似乎不起作用

时间:2019-01-16 08:37:51

标签: swift avqueueplayer cmtime avplayerlooper

我在设置AVQueuePlayer视频的偏移量时遇到问题。我尝试过seek(以设置视频的偏移量,但似乎无法正常工作。视频总是从0秒开始。其他要求是通过控制播放并循环播放,这样效果很好。

我很想从0秒以外的任何其他位置播放视频。

    func getVideoView() -> UIView
    {

    var videoViewContainer = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 375))

    let videoUrl = URL(string: "https://myvideourl.mp4")

    let item = AVPlayerItem(url: videoUrl!)

    player = AVQueuePlayer()

    playerLooper = AVPlayerLooper(player: player!, templateItem: item)

    let time =  CMTime(seconds: 17.000000, preferredTimescale: CMTimeScale(1))

    player?.seek(to: time, completionHandler: { (handler) in


    } )

    item.forwardPlaybackEndTime = CMTimeMake(20, 1) // For playing it for 20 Seconds

    let layer: AVPlayerLayer = AVPlayerLayer(player: player)


    layer.frame = videoViewContainer.bounds

    layer.videoGravity = AVLayerVideoGravity.resizeAspectFill

    videoViewContainer.layer.addSublayer(layer)

    player?.play()



    return videoViewContainer
}

1 个答案:

答案 0 :(得分:0)

我得到了答案。与AVPlayerLooper一起使用时,SeekTo似乎不起作用。

AVPlayerLooper本身具有属性时间范围。 以下示例

    playerLooper = AVPlayerLooper(player: self.player!,
                            templateItem: item!, 
                               timeRange: CMTimeRange(start: CMTime(seconds: Double(start), preferredTimescale: 1) , duration: CMTime(seconds: Double(duration), preferredTimescale: 1)))