1, 只用AVPlayer播放器
let player = AVPlayer(url: URL(string:path))
body中写法
VideoPlayer(player: player)
通过监听视频播放结束,手动重新播放
.onReceive(NotificationCenter.default.publisher(for: Notification.Name.AVPlayerItemDidPlayToEndTime), perform: { value in
self.player.seek(to: .zero)
self.player.play()
})
2,使用AVPlayerLooper, AVQueuePlayer,AVPlayerItem
let player = AVQueuePlayer()
let playItem = AVPlayerItem(url: URL(string:path))
self.playerLooper = AVPlayerLooper(player: player, templateItem: playItem)
Player.play()
默认情况下,播放的视频回到后台会暂停,要想重新播放,可以添加 @Environment(.scenePhase) private var scenePhase环境变量,然后
.onChange(of: scenePhase) { value in
switch value {
case .active:
// 回到前台
if player.status == .readyToPlay {
player.play()
}
default:
break
}
}
实现回到前台继续播放