-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInlinePlayerView.swift
More file actions
93 lines (78 loc) · 2.82 KB
/
InlinePlayerView.swift
File metadata and controls
93 lines (78 loc) · 2.82 KB
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
/*
See the LICENSE.txt file for this sample’s licensing information.
Abstract:
A view that display a simple inline video player with custom controls.
*/
import AVKit
import SwiftUI
struct InlinePlayerView: View {
@Environment(PlayerModel.self) private var model
var body: some View {
ZStack {
// A view that uses AVPlayerViewController to display the video content without controls.
VideoContentView()
// Custom inline controls to overlay on top of the video content.
InlineControlsView()
}
.onDisappear {
// If this view disappears, and it's not due to switching to full-window
// presentation, clear the model's loaded media.
if model.presentation != .fullWindow {
model.reset()
}
}
}
}
/// A view that defines a simple play/pause/replay button for the trailer player.
struct InlineControlsView: View {
@Environment(PlayerModel.self) private var player
@State private var isShowingControls = false
var body: some View {
VStack {
Image(systemName: player.isPlaying ? "pause.fill" : "play.fill")
.padding(8)
.background(.thinMaterial)
.clipShape(.circle)
}
.font(.largeTitle)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.contentShape(Rectangle())
.onTapGesture {
player.togglePlayback()
isShowingControls = true
// Execute the code below on the next runloop cycle.
Task { @MainActor in
if player.isPlaying {
dismissAfterDelay()
}
}
}
}
func dismissAfterDelay() {
Task {
try! await Task.sleep(for: .seconds(3.0))
withAnimation(.easeOut(duration: 0.3)) {
isShowingControls = false
}
}
}
}
/// A view that presents the video content of an player object.
///
/// This class is a view controller representable type that adapts the interface
/// of AVPlayerViewController. It disables the view controller's default controls
/// so it can draw custom controls over the video content.
private struct VideoContentView: UIViewControllerRepresentable {
@Environment(PlayerModel.self) private var model
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = model.makePlayerViewController()
// Disable the default system playback controls.
controller.showsPlaybackControls = false
return controller
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {}
}
#Preview {
InlineControlsView()
.environment(PlayerModel())
}