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