如何在AVPlayer替换当前AVPlayerItem时删除黑框?

时间:2018-06-05 22:01:53

标签: ios swift avplayer avplayeritem

我试图像Instagram那样播放视频。每次用户点击屏幕我都应该播放下一个。我的问题是,每当我更换视频时,我都会在播放器上看到黑框。如果使用新手机录制视频,则黑框需要更长时间才能消失。

我每次在视频之间切换时都尝试更改AVPlayer但是它可以正常工作,但播放器在12或15个视频后崩溃或停止工作。

 func startPlayer() {
        configure()
        play()
    }

 func playNext() {  
       guard let dataSource = dataSource else { return }
        if playingIndex < dataSource.numberOfPlayerVideoViewItems() - 1 {
            playItem(playingIndex + 1)
            return
        }
        resetIndex()
        startPlayer()
    }

 fileprivate func configure() {
    guard let playerVideoItem = dataSource?.playerVideoView(self, playerVideoViewItemAt:playingIndex),
        let videoURL = playerVideoItem.itemURL else { return }

    NotificationCenter.default.removeObserver(self)
    if player == nil {
        player = AVPlayer.init(url: videoURL)
        player?.automaticallyWaitsToMinimizeStalling = true;

        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = bounds
        playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

        layer.addSublayer(playerLayer)
    } else {
        player?.replaceCurrentItem(with: AVPlayerItem.init(url: videoURL))
    }

    NotificationCenter.default.addObserver(self, selector: #selector(videoDidFinish(_:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: self.player?.currentItem)

    observeProgress(for: player!.currentItem!)
}

0 个答案:

没有答案