| 1 |
efrain |
1 |
//
|
|
|
2 |
// VideoPlayerView.swift
|
|
|
3 |
// twogetskills
|
|
|
4 |
//
|
|
|
5 |
// Created by Efrain Yanez Recanatini on 4/24/22.
|
|
|
6 |
//
|
|
|
7 |
import Foundation
|
|
|
8 |
import SwiftUI
|
|
|
9 |
import AVKit
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
struct VideoPlayerView: View {
|
|
|
13 |
@EnvironmentObject private var appNavigation : AppNavigation
|
|
|
14 |
@State private var timerActive : Bool = false
|
|
|
15 |
@State private var isCompleted : Bool = false
|
|
|
16 |
|
|
|
17 |
private let appData = AppData.sharedInstance
|
|
|
18 |
|
|
|
19 |
//@State private var backToGallery : Bool = false
|
|
|
20 |
|
|
|
21 |
private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
|
|
|
22 |
|
|
|
23 |
private var playerItem : AVPlayerItem
|
|
|
24 |
private var player : AVPlayer
|
|
|
25 |
private var slideModel : SlideModel
|
|
|
26 |
private var slideTitle : String
|
|
|
27 |
|
|
|
28 |
|
|
|
29 |
//@State private var goToGallery : Bool = false
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
init()
|
|
|
33 |
{
|
|
|
34 |
let slideDao = SlideDao.sharedInstance
|
|
|
35 |
self.slideModel = slideDao.selectByUuid(uuid: appData.slideUuidActive)
|
|
|
36 |
|
|
|
37 |
if self.slideModel.name.count > Constants.APP_BAR_TITLE_MAX_LENGTH {
|
|
|
38 |
slideTitle = String(Array(self.slideModel.name)[0...Constants.APP_BAR_TITLE_MAX_LENGTH]) + "..."
|
|
|
39 |
} else {
|
|
|
40 |
slideTitle = self.slideModel.name
|
|
|
41 |
}
|
|
|
42 |
|
|
|
43 |
|
|
|
44 |
/*
|
|
|
45 |
let sURL = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
|
|
|
46 |
|
|
|
47 |
let videoUrl = URL(string: sURL)
|
|
|
48 |
*/
|
|
|
49 |
|
|
|
50 |
let videoUrl = URL(string: slideModel.file)
|
|
|
51 |
|
|
|
52 |
|
|
|
53 |
let headerSecurity = HeaderSecurity()
|
|
|
54 |
|
|
|
55 |
let headers: [String: String] = [
|
|
|
56 |
Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
|
|
|
57 |
Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
|
|
|
58 |
Constants.HTTP_HEADER_SECURITY_TOKEN: appData.deviceUuid,
|
|
|
59 |
Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
|
|
|
60 |
Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
|
|
|
61 |
]
|
|
|
62 |
|
|
|
63 |
let assets = AVURLAsset(url: videoUrl!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
|
|
|
64 |
playerItem = AVPlayerItem(asset: assets)
|
|
|
65 |
|
|
|
66 |
/*
|
|
|
67 |
let assets = AVURLAsset(url: videoUrl!)
|
|
|
68 |
playerItem = AVPlayerItem(asset: assets)
|
|
|
69 |
*/
|
|
|
70 |
|
|
|
71 |
let newTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale:1)
|
|
|
72 |
|
|
|
73 |
player = AVPlayer(playerItem: playerItem)
|
|
|
74 |
player.seek(to: newTime)
|
|
|
75 |
|
|
|
76 |
|
|
|
77 |
self.timerActive = true
|
|
|
78 |
|
|
|
79 |
|
|
|
80 |
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
var body: some View {
|
|
|
84 |
VStack(spacing: 0) {
|
|
|
85 |
HStack {
|
|
|
86 |
Button(action: {
|
|
|
87 |
let dataService = DataService()
|
|
|
88 |
if self.isCompleted {
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
dataService.completeSlide(slide: slideModel)
|
|
|
92 |
} else {
|
|
|
93 |
dataService.incompleteSlide(slide: slideModel)
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
if self.timerActive {
|
|
|
97 |
self.timer.upstream.connect().cancel()
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
AppDelegate.orientationLock = UIInterfaceOrientationMask.portrait
|
|
|
101 |
UIDevice.current.setValue(UIInterfaceOrientation.portrait.rawValue, forKey: "orientation")
|
|
|
102 |
UIViewController.attemptRotationToDeviceOrientation()
|
|
|
103 |
|
|
|
104 |
|
|
|
105 |
withAnimation {
|
|
|
106 |
appNavigation.subpageActive = .gallery
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
}, label: {
|
|
|
110 |
|
|
|
111 |
|
|
|
112 |
Image(systemName: "chevron.backward")
|
|
|
113 |
.frame(width: 32, height: 32, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
|
|
|
114 |
.aspectRatio(contentMode: .fit)
|
|
|
115 |
.foregroundColor(Color("color_app_bar_foreground"))
|
|
|
116 |
})
|
|
|
117 |
.padding(.leading, 16)
|
|
|
118 |
|
|
|
119 |
Text(self.slideTitle)
|
|
|
120 |
.font(Font.custom(Config.FONT_NAME_REGULAR, size: Config.FONT_SIZE_APP_BAR_HEAD1 ))
|
|
|
121 |
.foregroundColor(Color("color_app_bar_foreground"))
|
|
|
122 |
.padding(.leading, 4)
|
|
|
123 |
|
|
|
124 |
Spacer()
|
|
|
125 |
}
|
|
|
126 |
.background(Color("color_app_bar_background"))
|
|
|
127 |
.edgesIgnoringSafeArea(.top)
|
|
|
128 |
.frame(height: 50)
|
|
|
129 |
Divider()
|
|
|
130 |
|
|
|
131 |
VideoPlayer(player: player)
|
|
|
132 |
.onAppear() {
|
|
|
133 |
player.play()
|
|
|
134 |
}
|
|
|
135 |
.onDisappear() {
|
|
|
136 |
player.pause()
|
|
|
137 |
}
|
|
|
138 |
.onChange(of: player.currentTime(), perform: { value in
|
|
|
139 |
if let currentItem = player.currentItem {
|
|
|
140 |
let duration = currentItem.asset.duration
|
|
|
141 |
|
|
|
142 |
print("duration : \(duration)")
|
|
|
143 |
print("value : \(value)")
|
|
|
144 |
}
|
|
|
145 |
|
|
|
146 |
})
|
|
|
147 |
.onReceive(timer, perform: { _ in
|
|
|
148 |
print("onReceive")
|
|
|
149 |
|
|
|
150 |
let duration = CMTimeGetSeconds(playerItem.asset.duration)
|
|
|
151 |
|
|
|
152 |
|
|
|
153 |
|
|
|
154 |
let currentTime = CMTimeGetSeconds(playerItem.currentTime())
|
|
|
155 |
|
|
|
156 |
|
|
|
157 |
let diference = duration - currentTime
|
|
|
158 |
if diference < 10 {
|
|
|
159 |
self.isCompleted = true;
|
|
|
160 |
self.timer.upstream.connect().cancel()
|
|
|
161 |
timerActive = false
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
})
|
|
|
165 |
|
|
|
166 |
|
|
|
167 |
//.frame(width: geometry.size.width, height: geometry.size.height, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
|
|
|
168 |
|
|
|
169 |
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
}
|
|
|
177 |
|
|
|
178 |
|
|
|
179 |
struct VideoPlayerView_Previews: PreviewProvider {
|
|
|
180 |
static var previews: some View {
|
|
|
181 |
VideoPlayerView()
|
|
|
182 |
}
|
|
|
183 |
}
|
|
|
184 |
|