问题
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