I have a AVPlayer that displays a video from a url, this is how I set it up:
fileprivate func setUpPlayer(cardModel : CardModel){
cardModel.urlStrings?.forEach({ (urlstring) in
guard let url = URL(string: urlstring) else {
return
}
urlStrings.append(url)
})
player = AVPlayer(url: urlStrings[0])
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspectFill
player.play()
self.playerView.layer.insertSublayer(playerLayer, at: 0).
}
As you can see at the beginning I create a list of urls, I want to display this urls whenever a touch event occurs:
This are the two snippets I created to display this list of video from the array of urls:
fileprivate func goToNextVideo(){
counter = counter + 1
counter = min(counter, cardModel!.urlStrings!.count - 1)
player = AVPlayer(url: urlStrings[counter])
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspectFill
player.play()
}
fileprivate func goToPreviousVideo(){
counter = counter - 1
counter = max(0, counter)
player = AVPlayer(url: urlStrings[counter])
playerLayer = AVPlayerLayer(player: player)
playerLayer.videoGravity = .resizeAspectFill
player.play()
}
This implementation doesn't seem to work, when I go to the next video I can only hear the audio but can't see the video, do you know how can I play videos using AVPlayer from a list of urls?
The following demo will play an array of video urls, forward and back. Unfortunately I could not get it to run in Xcode: 'error = AudioObjectRemovePropertyListenerBlock: can't remove a NULL listener proc' and I do not know how to fix the problem. However it runs ok using Terminal. You will need to add your own file paths to be converted to urls (or add video urls). Copy/paste the code into a file named 'videoPlayer.swift' and use the command line code shown below in Terminal.