'SwiftUI AVPlayer disappear when alert or confirmationDialog show

I have a AVPlayer wrapper to play video in loop in SwiftUI. However, after I active an alert or confirmationDialog, the player will play video once and then disappear. See the screen recording at https://youtube.com/shorts/pvMbMSbICgI?feature=share. Anyone have idea how to fix this issue?

The problem occurs on 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)
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source