SwiftUI AVPlayer исчезает, когда отображается предупреждение или подтверждение.
У меня есть оболочка AVPlayer для воспроизведения видео в цикле в SwiftUI. Однако после того, как я активирую предупреждение или диалоговое окно подтверждения, проигрыватель воспроизведет видео один раз, а затем исчезнет. Смотрите запись экрана на странице https://youtube.com/shorts/pvMbMSbICgI?feature=share . Кто-нибудь знает, как решить эту проблему?
Проблема возникает на ios 15.2.
import SwiftUI
struct ContentView: View {
@State var showConfirmationDialog = false
let videoURL = Bundle.main.url(forResource: "video", withExtension: "mp4")
var body: some View {
LoopVideoPlayerView(url: videoURL!, videoWidth: 720, videoHeight: 1280)
Button {
self.showConfirmationDialog = true
} label: {
Text("Delete")
}
.confirmationDialog(
"Delete the video?",
isPresented: $showConfirmationDialog){
Button {
// Delete the video object in cloud
} label: {
Text("Yes")
}
Button("No", role: .cancel) {
self.showConfirmationDialog = false
}
} message: {
Text("Delete the video?")
}
}
}
import SwiftUI
import AVFoundation
struct LoopVideoPlayerView : View {
private var videoAsset: AVAsset
private var videoScaledHeight : CGFloat = 50.0
private var player : AVQueuePlayer?
private var playerlooper : AVPlayerLooper?
init(url: URL, videoWidth:Int, videoHeight:Int){
self.videoAsset = AVURLAsset(url: url)
let playerItem = AVPlayerItem(asset: videoAsset)
self.player = AVQueuePlayer(playerItem: playerItem)
self.playerlooper = AVPlayerLooper(player: self.player!, templateItem: playerItem)
let screenWidth = UIScreen.main.bounds.width
self.videoScaledHeight = (screenWidth / CGFloat(videoWidth)) * CGFloat(videoHeight)
}
var body: some View {
PlayerView(player: self.player!)
.frame(height: videoScaledHeight)
.onAppear {
self.player?.play()
}
}
}
import SwiftUI
import Foundation
import UIKit
import AVFoundation
class VideoPlayerUIView: UIView {
private let playerLayer = AVPlayerLayer()
init(player: AVPlayer) {
super.init(frame: .zero)
playerLayer.player = player
playerLayer.videoGravity = .resizeAspect
playerLayer.contentsGravity = .resizeAspect
layer.addSublayer(playerLayer)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
playerLayer.frame = bounds
}
}
struct PlayerView: UIViewRepresentable {
@State var player: AVPlayer
func updateUIView(_ uiView: UIView, context: UIViewRepresentableContext<PlayerView>) {
}
func makeUIView(context: Context) -> UIView {
return VideoPlayerUIView(player: player)
}
}