AVPlayer stuck in video but audio works

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 // Use the AVAsset playable property to detect whether the asset can be played if !asset.playable { let localizedDescription = "Item cannot be played,Item cannot be played description" let localizedFailureReason = "The assets tracks were loaded, but could not be made playable,Item cannot be played failure reason" let userInfo = [NSLocalizedDescriptionKey: localizedDescription, NSLocalizedFailureReasonErrorKey: localizedFailureReason] let error = NSError(domain: "domain", code: 0, userInfo: userInfo) self.videoPlayerDelegate?.videoPlayer?(self, playerItemStatusDidFail: error) self.cleanPlayer() return } // At this point we're ready to set up for playback of the asset. Stop observing if let _ = self.player?.currentItem { self.cleanPlayer() } if asset.URL.absoluteString != safeURL.absoluteString { return } var error: NSError? let status = asset.statusOfValueForKey(self.assetKeyTracks, error: &error) var playerItem = AVPlayerItem(URL: safeURL) if status == .Loaded { playerItem = AVPlayerItem(asset: asset) } else { // You should deal with the error appropriately.If Loaded fails, create an AVPlayerItem directly from the URL playerItem = AVPlayerItem(URL: safeURL) } self.player = self.playerWithPlayerItem(playerItem) self.registerMonitoring() self.registerNotification() self.addTimeObserver() completionBlock?(loadURLString: playerURL.absoluteString) }) } 

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:

enter image description here

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

enter image description here

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.

+7
ios objective-c swift video avfoundation
source share
2 answers

Try this approach as I ran into the same problem and solve

 player.pause() player.currentItem?.seek(to: CMTime(), completionHandler: { (_) in player.play() }) 
+2
source share

Same user code nferocious76, but in Objective C

 AVPlayerItem *p = [player currentItem]; [p seekToTime:p.currentTime completionHandler:^(BOOL finished){ if (finished){ [self->player play]; } }]; 
0
source share

All Articles