AVPlayer shows black screen when playing multiple Videos in Swift3 iOS

耗尽温柔 提交于 2019-11-26 18:39:23

问题


I am working on Video player based Application, where I have to play multiple Videos using AVPlayer. Each video has to play with some CIFilter effect so I am using VideoComposition for that.

My problem is when there is more than one Video in my VideoArray then first Video is playing perfectly with audio, but when it’s coming to the next Video then AVPlayer shows black screen but audio sound of the Video is coming. I am invoking the below common method whenever Video is playing:

private func createPlayerForVideoIndex(index: Int){

        guard let videoComposition = videoFilterComposition else { return }

        self.player?.pause()
        self.audioPlayer?.pause()

        playerLayer?.removeFromSuperlayer()

        let video = videoClipsArray.object(at: index) as! VideoFileModel
        playerItem = AVPlayerItem(url:NSURL(fileURLWithPath: video.localFilePath) as URL)
        playerItem?.videoComposition = videoComposition

        self.player = AVPlayer(playerItem: playerItem)

        playerLayer = AVPlayerLayer(player: player)

        playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
        playerLayer?.frame = CGRect(x:0, y:67, width:self.view.frame.size.width, height:250)

        self.view.layer.addSublayer(playerLayer!)



            if !myScrollView.isDescendant(of: self.view) {

                myScrollView.frame = (playerLayer?.frame)!
                myScrollView.delegate = self
                myScrollView.isPagingEnabled = true
                myScrollView.showsHorizontalScrollIndicator = false
                myScrollView.showsVerticalScrollIndicator = false
                myScrollView.backgroundColor = UIColor.clear

                self.view.addSubview(myScrollView)
            }

            self.view.bringSubview(toFront: self.pageControl)


            self.myScrollView.contentSize = CGSize(width:self.myScrollView.frame.size.width * 4,height: self.myScrollView.frame.size.height)

            let startSeconds : Int64 = Int64(video.leftRangeValue)
            let targetTime:CMTime = CMTimeMake(startSeconds, 1)
            self.player?.seek(to: targetTime)

            let endSeconds : Int64 = Int64(video.rightRangeValue)
            self.player?.currentItem?.forwardPlaybackEndTime = CMTimeMake(endSeconds, 1)

            self.player?.volume = videoVolumeSlider.value

            self.player?.play()
            self.audioPlayer?.play()

            self.player?.rate = video.slowFastMotionRate

    }

I assume, there should have the problem with above method which is calling every time for each Video (I mean with next video).

Can anyone please suggest me.

来源:https://stackoverflow.com/questions/54745359/avplayer-shows-black-screen-when-playing-multiple-videos-in-swift3-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!