Hi there,
I'm attempting to implement an Instagram-like gallery using a UICollectionView that houses a UIScrollView with paging enabled. During a configure
method call, I iterate over an array of Media
objects that are added to the UIScrollView's subview using an offset.
I'm having trouble, in particular, using AVPlayerViewController to show multiple videos in a single UIScrollView. Below is my particular case for the "video" part of my configure method.
let xPosition = scrollView.bounds.width * CGFloat(i)
...
case "video":
print("Configuring media with the following video url: \(media[i].url)")
guard let url = URL(string: media[i].url) else {
print("Unable to create URL from string!")
return
}
let player = AVPlayer(url: url)
playerController.player = player
playerController.view.frame = CGRect(x: xPosition, y: 0, width: scrollView.bounds.width, height: scrollView.bounds.height)
playerController.videoGravity = .resizeAspect
playerController.showsPlaybackControls = true
playerController.view.backgroundColor = .white
scrollView.addSubview(playerController.view)
...
One thing to note is that playerController
is a class property of type AVPlayerViewController that I'm attempting to reuse. I believe what is happening here, is that every iteration of the for loop is causing the single playerController
's frame to get pushed further and further left as more media items are iterated over. Because of this, I don't know if there's a particular way to reuse the same AVPlayerViewController, or if I need to use one for each video.
I prefer to use AVPlayerViewController for its "out-of-the-box" readiness regarding controls, etc.
Thanks!