I want to use AVPlayerViewController
to display the video but it should be in auto-play mode.
Previously I was using AVPlayer for that and listening to the .AVPlayerItemDidPlayToEndTime
notification but I wonder if there is a better way? eg. using AVPlayerLooper for instance so I don't have to use that .AVPlayerItemDidPlayToEndTime
anymore
I wrote something like this but it is not working - I have a black screen with video controls - probably because AVPlayerViewController does not have any playable content...
struct VideoPlayerQueuedView: UIViewControllerRepresentable {
let videoUrl: URL
func makeUIViewController(context: Context) -> AVPlayerViewController {
let queuePlayer = AVQueuePlayer()
let playerViewController = AVPlayerViewController()
// Create an AVPlayerItem from the videoUrl
let playerItem = AVPlayerItem(url: videoUrl)
// Create an AVPlayerLooper with the queuePlayer and the playerItem as the template item
let playerLooper = AVPlayerLooper(player: queuePlayer, templateItem: playerItem)
// Set the player property of AVPlayerViewController
playerViewController.player = queuePlayer
return playerViewController
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {
// Update the video player if needed
}
}