-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathContentView.swift
64 lines (52 loc) · 2.05 KB
/
ContentView.swift
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
//
// ContentView.swift
// VisionPro-Example
//
// Created by shayanbo on 2023/8/5.
//
import SwiftUI
import VideoPlayerContainer
import AVKit
struct ContentView: View {
/// create Context and let it have the same lifecycle with its enclosing underlying view
/// Besides that the PlayerWidget makes the context accessible by its Widget,
/// we need make it accessible by toolbar Widgets as well which is out of VideoPlayerContainer
///
@StateObject var context = Context()
var body: some View {
PlayerWidget(context)
.onAppear {
guard let url = Bundle.main.url(forResource: "demo", withExtension: "mp4") else {
fatalError()
}
context.render.player.replaceCurrentItem(with: AVPlayerItem(url: url))
context.render.player.play()
}
.toolbar {
ToolbarItem(placement: .bottomOrnament) {
HStack {
/// leading space
Spacer().frame(width: 10)
/// Widgets displaying current time
TimelineWidget()
/// Widgets displaying status of playback
PlaybackWidget()
/// Widgets displaying the progress of playing video
SeekBarWidget()
/// Widgets supporting change of video rate
RateWidget()
/// Widgets displaying duration
DurationWidget()
/// trailing space
Spacer().frame(width: 10)
}
}
}
/// make it accessible inside toolbar Widgets
/// Generally, we can set Context instance as the whole View tree's environmentObject to enable all of subviews inside of the View to access the Context instance.
.environmentObject(context)
}
}
#Preview {
ContentView()
}