Saturday, June 11, 2016

AVPlayer play the video,can play a sound, but video images stuck

Leave a Comment

I go to create AVPlayerItem through AVURLAsset,This is my code:

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 display video in my View,This is my code:

// MARK: - Property

var player: AVPlayer? {     get {         return playerLayer.player     }      set {         playerLayer.player = newValue     } }  var playerLayer: AVPlayerLayer {     return layer as! AVPlayerLayer } 

When displaying video after completion of loading

self.videoPlayer?.loadPlayer({         [weak self](loadURLString) in         if let strongSelf = self {             strongSelf.player = strongSelf.videoPlayer?.player             strongSelf.startPlay()         }     }) 

Call seekToTime method to specify the play:

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 pictures of interface is stuck

enter image description here

enter image description here

The first picture, the video there is sound, but the interface is stuck

The second picture, video not picture,but have a sound

Now I have a question, when I call seekToTime method is completed, sometimes video there is sound, but the interface gets stuck,sometimes, video not picture, But not every time,I tried to callCALayer setNeedsDisplay method,to update AVPlayerLayer picture, but no effect,Therefore, I do not know with what method to know where is the problem,Hope someone can help me.

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment