2017-06-06 79 views
2

我試圖反向再現添加到視頻當中我與AVPlayer打:問題具有反向AVPlayer倒放

let videoURL = Bundle.main.url(forResource: "video", withExtension: "mov") 
videoPlayer = AVPlayer(url:videoURL!) 
let playerLayer = AVPlayerLayer(player: videoPlayer) 
playerLayer.frame = self.view.frame 
videoView.layer.addSublayer(playerLayer) 
videoPlayer.play() 

我搜查,發現如果我改變AVPlayer的速度-1影片反向播放模式:

func reverseVideo() { 

     videoPlayer.play() 
     videoPlayer.rate = -1 
    } 

這樣確實不錯,但倒放含有大量的滯後和不順利播放,有任何可能的方式來解決這個問題。我在這裏看過其他話題,但沒有幫助。

+0

你看到的滯後只在開始試圖反向打? –

+0

@TarunTyagi是 –

+0

您是否嘗試調用'playbackLikelyToKeepUp'後觸發'reverseVideo'方法。你可以設置一個觀察者來跟蹤這個值。這是官方的[文檔](https://developer.apple.com/documentation/avfoundation/avplayeritem/1390348-playbacklikelytokeepup)。 – Ayazmon

回答

1

我不確定下面是否會正常工作,因爲我之前沒有嘗試播放資產。我認爲,在開始比賽之前嘗試尋求結束是一個好主意。

player.seek(to: player.currentItem!.duration, 
      toleranceBefore: kCMTimeZero, 
      toleranceAfter: kCMTimeZero, 
      completionHandler: { (finished) in 
     player.play() 
     player.rate = -1 
    }) 
+0

謝謝,但仍然滯後! –

+0

我不確定它是否會有幫助,但是可以在說'player.play()'之前嘗試指定'player.rate = -1'嗎? –

2

這段代碼適用於我在avplayer中從本地文件系統反向播放視頻。

let duration = (self.playeritem?.asset.duration)! 
let durationSec = CMTimeGetSeconds((self.playeritem?.asset.duration)!) 
self.avPlayer?.seek(to: duration, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) 
self.avPlayer?.rate = -1 
+0

這就是Tarun Tyagi和現在的工作一樣的問題,'durationSec'在這裏是無用的 –

1

嘗試用 videoPlayer.playImmediately(atRate: -1)

+0

同樣的效果!仍然有滯後,不平穩,你能回答嗎? : https://stackoverflow.com/questions/44434586/achieve-smooth-video-scrubbing-with-avplayer –

1
var durTime: CMTime = queuePlayer.currentItem.asset.duration 
durationTime = CMTimeGetSeconds(durTime) 

if CMTIME_IS_VALID(durTime) { 
    queuePlayer.seek(toTime: durTime, toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) 
} 
else { 
    print("In valid time") 
} 

AVPlayer的現在設定的速度更換 videoPlayer.play() videoPlayer.rate = -1到負值

queuePlayer.rate = -1 
+0

我該如何讓它更平滑? –