I am trying to show a looping video then allow the user to tap a button to show the next or previous looping video.
Why doesn't @Binding update this? and what is the right/best way to do this?
I am using a UIViewRepresentable. Here is my code...
struct Player: UIViewRepresentable {
var videoFileNames: [String]
@Binding var currentItem: Int
func makeUIView(context: Context) -> UIView {
return PlayerView(frame: .zero, videoFileNames: videoFileNames, currentItem: currentItem)
}
func updateUIView(_ uiView: UIView, context: Context) {
}
}
class PlayerView: UIView {
private let playerLayer = AVPlayerLayer()
private var playerLooper: AVPlayerLooper?
init(frame: CGRect, videoFileNames: [String], currentItem: Int) {
super.init(frame: frame)
var playerItems = [AVPlayerItem]()
let player = AVQueuePlayer(items: playerItems)
for videoFileName in videoFileNames {
guard let path = Bundle.main.path(forResource: videoFileName, ofType: "mp4") else {
return
}
let videoURL = NSURL(fileURLWithPath: path)
let playerItem = AVPlayerItem(url: videoURL as URL)
playerItems.append(playerItem)
}
playerLooper = AVPlayerLooper(player: player, templateItem: playerItems[currentItem])
player.volume = 0
player.play()
playerLayer.player = player
playerLayer.videoGravity = .resizeAspectFill
layer.addSublayer(playerLayer)
}
required init?(coder: NSCoder) {
super.init(coder: coder)
}
override func layoutSubviews() {
super.layoutSubviews()
playerLayer.frame = bounds
}
}