How to Play a video in AVPlayer for a specific duration in Swift3 iOS

走远了吗. 提交于 2019-12-11 14:18:38

问题


I am creating a Video application in Swift3. Where we have a list of Video files in a TableView list. For each Video we have given Range Slider option for user to select Range of the Video. Now I am trying to play the Video for that specific range, selected by the User.

I am using below code to start a Video from 4 Seconds to 8 Seconds using CMTimeMake but not playing correctly.

let targetTime:CMTime = CMTimeMake(4, 1)////Video start
self.player?.seek(to: targetTime)

self.player?.currentItem?.forwardPlaybackEndTime = CMTimeMake(8, 1)//Video stop
self.player?.play()

Can anyone help me where I am doing wrong. Thank you.


回答1:


Maybe try this:

let item = AVPlayerItem(url: path) 
let player = AVPlayer(playerItem: item)
player.seek(to: CMTimeMake(4, 1)) 
item.forwardPlaybackEndTime = CMTimeMake(8, 1) 
self.player.play()

Edit: This looks pretty similar to the original code, minus the use of .currentItem which I'm guessing might be the issue.



来源:https://stackoverflow.com/questions/50810183/how-to-play-a-video-in-avplayer-for-a-specific-duration-in-swift3-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!