-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayerSettingsButton.swift
More file actions
156 lines (136 loc) · 5.56 KB
/
PlayerSettingsButton.swift
File metadata and controls
156 lines (136 loc) · 5.56 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import SwiftUI
@available(iOS 13.0, *)
struct PlayerSettingsButton: View {
@State private var showOptions = false
@State private var currentMenu: SettingsMenu = .main
@EnvironmentObject var player: TPStreamPlayerObservable
private var playerConfig: TPStreamPlayerConfiguration
init(playerConfig: TPStreamPlayerConfiguration){
self.playerConfig = playerConfig
}
var body: some View {
HStack {
Spacer()
if playerConfig.showSettingsButton {
Button(action: {
showOptions = true
currentMenu = .main
}) {
Image("settings", bundle: bundle)
.resizable()
.frame(width: 16, height: 16)
}
.padding([.trailing, .top], 12)
.actionSheet(isPresented: $showOptions, content: settingsActionSheet)
}
}
}
private func settingsActionSheet() -> ActionSheet {
switch currentMenu {
case .main:
return ActionSheet(
title: Text("Settings"),
message: nil,
buttons: getMainActionSheetButtons()
)
case .playbackSpeed:
return ActionSheet(
title: Text("Playback Speed"),
message: nil,
buttons: playbackSpeedOptions() + [.cancel()]
)
case .videoQuality:
return ActionSheet(
title: Text("Video Quality"),
message: Text("Video quality adjusts based on your internet speed. Your selection sets the highest possible quality."),
buttons: videoQualityOptions() + [.cancel()]
)
case .downloadQuality:
return ActionSheet(
title: Text("Download Quality"),
message: Text("Video quality adjusts based on your internet speed. Your selection sets the highest possible quality."),
buttons: downloadQualityOptions() + [.cancel()]
)
}
}
private func getMainActionSheetButtons() -> [ActionSheet.Button] {
var actionButtons: [ActionSheet.Button] = []
if playerConfig.enablePlaybackSpeed {
actionButtons.append(playbackSpeedButton())
}
if !player.player.isPlaybackOffline {
addOnlinePlaybackButtons(to: &actionButtons)
}
actionButtons.append(.cancel())
return actionButtons
}
private func addOnlinePlaybackButtons(to buttons: inout [ActionSheet.Button]) {
if playerConfig.showResolutionOptions {
buttons.append(videoQualityButton())
}
if playerConfig.showDownloadOption {
buttons.append(downloadQualityButton())
}
}
private func playbackSpeedButton() -> ActionSheet.Button {
return .default(Text("Playback Speed - \(player.observedCurrentPlaybackSpeed.label)")) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.showOptions = true
self.currentMenu = .playbackSpeed
}
}
}
private func videoQualityButton() -> ActionSheet.Button {
let currentLabel = player.currentVideoQuality.map { VideoQualityUtils.getDisplayLabel(for: $0) } ?? "Auto"
return .default(Text("Video Quality - \(currentLabel)")) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.showOptions = true
self.currentMenu = .videoQuality
}
}
}
private func downloadQualityButton() -> ActionSheet.Button {
return .default(Text("Download")) {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.showOptions = true
self.currentMenu = .downloadQuality
}
}
}
private func playbackSpeedOptions() -> [ActionSheet.Button] {
let playbackSpeeds = PlaybackSpeed.allCases
return playbackSpeeds.map { speed in
.default(Text(speed.label)) {
player.changePlaybackSpeed(speed)
}
}
}
private func videoQualityOptions() -> [ActionSheet.Button] {
return player.availableVideoQualities.map { videoQuality in
.default(Text(VideoQualityUtils.getDisplayLabel(for: videoQuality))) {
player.changeVideoQuality(videoQuality)
}
}
}
private func downloadQualityOptions() -> [ActionSheet.Button] {
var availableVideoQualities = player.availableVideoQualities
// Remove Auto Quality from the Array
availableVideoQualities.remove(at: 0)
return availableVideoQualities.map { downloadQuality in
.default(Text(downloadQuality.resolution)) {
do {
try TPStreamsDownloadManager.shared.enqueueDownload(
asset: player.asset!,
accessToken: player.player.accessToken,
videoQuality: downloadQuality,
metadata: playerConfig.downloadMetadata,
offlineLicenseDurationSeconds: playerConfig.licenseDurationSeconds
)
} catch {
print("Error downloading video: \(error)")
}
}
}
}
}
enum SettingsMenu { case main, playbackSpeed, videoQuality, downloadQuality }