Search code examples
ios8avplayerfreezeavmutablecomposition

AVPlayerItem videoComposition freeze IOS8


I am playing a AVMutableVideoComposition with AVPlayer and since IOS8 everything was perfectly fine. But now the video start playing and after 4 or 5 seconds it stop ,like buffering or something like that, the sound keeps playing and when the video ends the AVPlayer loops and play it fine without stops.

I have no clue for fixing this issue.

Any help would be appreciate,

Thank you


Solution

  • I had a same issue, but I got the solution.

    You should play after playerItem's status is changed to .ReadyToPlay.

    I also answered here, that issue is similar to your issue.

    Please see as below.

    func startVideoPlayer() {
        let playerItem = AVPlayerItem(asset: self.composition!)
        playerItem.videoComposition = self.videoComposition!
    
        let player = AVPlayer(playerItem: playerItem)
        player.actionAtItemEnd = .None
    
        videoPlayerLayer = AVPlayerLayer(player: player)
        videoPlayerLayer!.frame = self.bounds
    
        /* add playerItem's observer */
        player.addObserver(self, forKeyPath: "player.currentItem.status", options: .New, context: nil)
    
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerItemDidReachEnd:", name: AVPlayerItemDidPlayToEndTimeNotification, object: playerItem);
    
        self.layer.addSublayer(videoPlayerLayer!)
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        if keyPath != nil && keyPath! == "player.currentItem.status" {
            if let newValue = change?[NSKeyValueChangeNewKey] {
                if AVPlayerStatus(rawValue: newValue as! Int) == .ReadyToPlay {
                    playVideo() /* play after status is changed to .ReadyToPlay */
                }
            }
        } else {
            super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        }
    }    
    
    func playerItemDidReachEnd(notification: NSNotification) {
        let playerItem = notification.object as! AVPlayerItem
        playerItem.seekToTime(kCMTimeZero)
    
        playVideo()
    } 
    
    func playVideo() {
        videoPlayerLayer?.player!.play()
    }