I am playing an array of videos using AVQueuePlayer
, but after the last video I see AVQueuePlayer
trying to play the next video when there isn't any left. And what I'm trying to do is to toggle a boolean after the last item finished playing to show a sheet view. Can you guy please help me with this? Thanks for your attention. I’m looking forward to your reply. Here is my code:
`struct Button : View {
@Binding var player : AVQueuePlayer
@Binding var isplaying : Bool
@State var showEnd = false
var removal: (() -> Void)? = nil
var body : some View {
Button(action: {
self.player.advanceToNextItem()
}) {
Image(systemName: self.isplaying ? "pause.fill" : "play.fill")
.font(.title)
.foregroundColor(.black)
.padding(20)
}
}
}
struct VideoPlayerS : UIViewControllerRepresentable {
@State var showEnd = false
var work : WorkoutDeS
@Binding var player : AVQueuePlayer
var playerLayer = AVPlayerLayer()
public func makeUIViewController(context: Context) -> AVPlayerViewController {
let items = [
AVPlayerItem(url: URL(fileURLWithPath: String(work.url1))),
AVPlayerItem(url: URL(fileURLWithPath: String(work.url2)))
]
let player = AVQueuePlayer(items: items)
let controller = AVPlayerViewController()
DispatchQueue.main.async {
self.player = player
}
controller.player = player
controller.videoGravity = .resizeAspectFill
player.actionAtItemEnd = .none
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: self.player.currentItem, queue: .main) { _ in
self.player.seek(to: CMTime.zero)
self.player.play()
}
player.play()
return controller
}`
Set an observer on the queue player's currentItem
. When the observer method is called and there are no items in the player, it has played its last item.