I want to create AVPlayerItem via AVURLAsset , my code is:
let asset = AVURLAsset(URL: safeURL, options: [AVURLAssetPreferPreciseDurationAndTimingKey: true]) asset.loadValuesAsynchronouslyForKeys([assetKeyPlayable, self.assetKeyTracks, self.assetKeyHasProtectedContent]) { () -> Void in dispatch_async(dispatch_get_main_queue(), { () -> Void in
Add AVPlayerLayer video display in my view, my code is:
// MARK: - Property
var player: AVPlayer? { get { return playerLayer.player } set { playerLayer.player = newValue } } var playerLayer: AVPlayerLayer { return layer as! AVPlayerLayer }
When displaying video after download is complete
self.videoPlayer?.loadPlayer({ [weak self](loadURLString) in if let strongSelf = self { strongSelf.player = strongSelf.videoPlayer?.player strongSelf.startPlay() } })
Call the seekToTime method to specify playback:
self.player?.currentItem?.seekToTime(CMTimeMakeWithSeconds(time, Int32(NSEC_PER_SEC)), toleranceBefore: kCMTimeZero, toleranceAfter: kCMTimeZero) { [weak self] finished in if let weakSelf = self { if weakSelf.isPlaying { weakSelf.videoPlayerDelegate?.videoPlayerDidplayerItemSeekToTime?(weakSelf) } } }
Some snapshots of the stuck interface:

In the first picture, the sound is audible, but the interface is stuck.

In the second image, the video works, but I do not get sound.
My question is:
When I call the seekToTime method upon completion, sometimes the video has sound, but the interface gets stuck, sometimes the video works. I tried calling the CALayer methods CALayer to update the AVPlayerLayer image, but that didn't help. I do not know what to do next, I would be grateful for any help.
ios objective-c swift video avfoundation
Shannon yang
source share