Reputation: 1
As the title suggests, I open the video in a new window, but when I try to close it while it's playing, the window closes but the video seems to continue to play in the background. I can tell this as the audio is continuing.
Here's the code
import Foundation
import SwiftUI
import AVKit
struct VideoPlayerView: UIViewControllerRepresentable {
var videoURL: URL
func makeUIViewController(context: Context) -> UIViewController {
// Wrap AVPlayerViewController in a plain UIViewController to manage its view lifecycle more directly
let containerViewController = UIViewController()
let playerViewController = AVPlayerViewController()
let player = AVPlayer(url: videoURL)
// Configure the player
playerViewController.player = player
playerViewController.showsPlaybackControls = true
// Ensure the player view controller's view fits the container view
playerViewController.view.frame = containerViewController.view.bounds
playerViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Add the player view controller as a child to the container view controller
containerViewController.addChild(playerViewController)
containerViewController.view.addSubview(playerViewController.view)
playerViewController.didMove(toParent: containerViewController)
return containerViewController
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) {
// Perform any updates to the UI or player if necessary
}
}
struct VideoWindow: View {
var body: some View {
// Ensure the video URL is correctly provided
VideoPlayerView(videoURL: Bundle.main.url(forResource: "TimesSquare2DShort", withExtension: "mov")!)
.edgesIgnoringSafeArea(.all) // Optional, based on layout needs
}
}
I've tried to debugprint, and it seems that .onDisappear is not being registered even when the window is closed.
Upvotes: 0
Views: 182