1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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?()
}
}