如何在 AVQueuePlayer 中播放完最后一个项目后显示 sheet 视图?
How to show sheet view after last item finished playing in AVQueuePlayer?
我正在使用 AVQueuePlayer
播放一系列视频,但在播放完最后一个视频后,我看到 AVQueuePlayer
试图播放下一个视频,但没有剩余视频。我想要做的是在最后一个项目播放完毕后切换一个布尔值以显示 sheet 视图。你能帮我解决这个问题吗?感谢您的关注。我期待着您的回复。这是我的代码:
`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
}`
在队列玩家的 currentItem
上设置一个观察者。当调用观察者方法并且播放器中没有任何项目时,它已经播放了最后一个项目。
我正在使用 AVQueuePlayer
播放一系列视频,但在播放完最后一个视频后,我看到 AVQueuePlayer
试图播放下一个视频,但没有剩余视频。我想要做的是在最后一个项目播放完毕后切换一个布尔值以显示 sheet 视图。你能帮我解决这个问题吗?感谢您的关注。我期待着您的回复。这是我的代码:
`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
}`
在队列玩家的 currentItem
上设置一个观察者。当调用观察者方法并且播放器中没有任何项目时,它已经播放了最后一个项目。