import AVKit import Combine import SwiftUI private struct VideoPlaybackRequest: Identifiable { let url: URL var id: String { url.absoluteString } } struct VideoLinks: View { let urls: [URL] @State private var selectedVideo: VideoPlaybackRequest? var body: some View { VStack(spacing: 8) { ForEach(urls, id: \.absoluteString) { url in Button { selectedVideo = VideoPlaybackRequest(url: url) } label: { Label("Tap here to play the video", systemImage: "play.rectangle") .appFont(.subheadline, weight: .medium) .frame(maxWidth: .infinity, minHeight: 48) .modifier(MediaControlSurface()) } .buttonStyle(.plain) .accessibilityHint("Opens video playback in a separate screen") } } .background { VideoPlayerPresenter(request: $selectedVideo) } } } #if DEBUG #Preview("Video link") { PreviewHost { _ in VideoLinks(urls: [MediaPreviewContent.videoURL]) .padding() } } #endif private struct VideoPlayerPresenter: UIViewControllerRepresentable { @Binding var request: VideoPlaybackRequest? func makeUIViewController(context: Context) -> VideoPresentingController { let presenter = VideoPresentingController() presenter.didAppear = { [weak coordinator = context.coordinator] in coordinator?.finishIfDismissed() } return presenter } func updateUIViewController(_ presenter: VideoPresentingController, context: Context) { guard let request, !context.coordinator.isPresenting else { return } context.coordinator.isPresenting = true DispatchQueue.main.async { guard presenter.viewIfLoaded?.window != nil, presenter.presentedViewController == nil else { context.coordinator.isPresenting = false self.request = nil return } let controller = AVPlayerViewController() let item = AVPlayerItem(url: request.url) controller.player = AVPlayer(playerItem: item) context.coordinator.observe(item, controller: controller) presenter.present(controller, animated: true) { controller.presentationController?.delegate = context.coordinator context.coordinator.didPresent(controller) controller.player?.play() self.request = nil context.coordinator.isPresenting = false } } } func makeCoordinator() -> Coordinator { Coordinator() } final class Coordinator: NSObject, UIAdaptivePresentationControllerDelegate { var isPresenting = false private var observation: AnyCancellable? private var playbackFailed = false private var didShowFailure = false private var presentationFinished = false private weak var currentController: AVPlayerViewController? func observe(_ item: AVPlayerItem, controller: AVPlayerViewController) { currentController = controller playbackFailed = false didShowFailure = false presentationFinished = false observation = item.publisher(for: \.status) .receive(on: DispatchQueue.main) .sink { [weak self, weak controller] status in guard status == .failed, let self, let controller else { return } self.playbackFailed = true self.showFailureIfNeeded(on: controller) } } func didPresent(_ controller: AVPlayerViewController) { presentationFinished = true showFailureIfNeeded(on: controller) } private func showFailureIfNeeded(on controller: AVPlayerViewController) { guard presentationFinished, playbackFailed, !didShowFailure, controller.presentingViewController != nil else { return } didShowFailure = true let alert = UIAlertController( title: "Video could not be played", message: "Check your connection and try again.", preferredStyle: .alert) alert.addAction(UIAlertAction(title: "OK", style: .default)) controller.present(alert, animated: true) } func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { finish() } func finishIfDismissed() { guard currentController?.presentingViewController == nil else { return } finish() } private func finish() { currentController?.player?.pause() currentController?.player?.replaceCurrentItem(with: nil) currentController = nil observation = nil isPresenting = false } } } private final class VideoPresentingController: UIViewController { var didAppear: (() -> Void)? override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) didAppear?() } }