Proyectos de Subversion Iphone Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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
    @Environment(\.presentationMode)
14
       var presentationMode: Binding
15
 
16
    //@State private var backToGallery : Bool = false
17
    @State private var timerActive : Bool = false
18
    @State private var isCompleted : Bool = false
19
    private let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
20
 
21
    private var playerItem : AVPlayerItem
22
    private var player : AVPlayer
23
    private var slideModel : SlideModel
24
    private var position : Int
25
 
26
    private let preference = Preference.sharedInstance
27
 
28
 
29
    private let colorBackgroundTopic = UIColor(hex: Config.COLOR_BACKGROUND_TOPIC)
30
 
31
    private let colorAppTextView = UIColor(hex: Config.COLOR_APP_TEXT_VIEW_TITLE)
32
 
33
    private let colorAppBackground = UIColor(hex: Config.COLOR_APP_BAR)
34
 
35
    //@State private var goToGallery : Bool = false
36
 
37
 
38
    init(slideModel : SlideModel, position : Int)
39
    {
40
 
41
 
42
        print("video player init")
43
 
44
        self.slideModel = slideModel
45
        self.position = position
46
 
47
        /*
48
        let sURL = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
49
 
50
        let videoUrl = URL(string: sURL)
51
*/
52
 
53
        let videoUrl = URL(string: slideModel.file)
54
 
55
 
56
        let headerSecurity = HeaderSecurity()
57
 
58
        let headers: [String: String] = [
59
            Constants.HTTP_HEADER_ACCEPT: Constants.HTTP_HEADER_ACCEPT_VALUE,
60
            Constants.HTTP_HEADER_SECURITY_RAND: String(headerSecurity.rand),
61
            Constants.HTTP_HEADER_SECURITY_TOKEN: preference.deviceUuid,
62
            Constants.HTTP_HEADER_SECURITY_CREATED: String(headerSecurity.created) ,
63
            Constants.HTTP_HEADER_SECURITY_SECRET: headerSecurity.secret,
64
        ]
65
 
66
        let assets = AVURLAsset(url: videoUrl!, options: ["AVURLAssetHTTPHeaderFieldsKey": headers])
67
        playerItem = AVPlayerItem(asset: assets)
68
 
69
        /*
70
        let assets = AVURLAsset(url: videoUrl!)
71
        playerItem = AVPlayerItem(asset: assets)
72
        */
73
 
74
        let newTime : CMTime = CMTimeMakeWithSeconds(0, preferredTimescale:1)
75
 
76
        player = AVPlayer(playerItem: playerItem)
77
        player.seek(to: newTime)
78
 
79
 
80
        self.timerActive = true
81
 
82
 
83
 
84
    }
85
 
86
    var body: some View {
87
        //GeometryReader { geometry in
88
            VStack {
89
                /*NavigationLink("", destination: GridGalleryView(capsuleUuid: self.slideModel.capsuleUuid, position: self.position), isActive: self.$goToGallery).frame( height: 0)*/
90
 
91
                VideoPlayer(player: player)
92
                    .onAppear() {
93
 
94
                        player.play()
95
                    }
96
                    .onDisappear() {
97
                        player.pause()
98
                    }
99
                    .onChange(of: player.currentTime(), perform: { value in
100
                        print("onchange")
101
                        if let currentItem = player.currentItem {
102
                            let duration = currentItem.asset.duration
103
 
104
                            print("duration : \(duration)")
105
                            print("value : \(value)")
106
                        }
107
 
108
                    })
109
                    .onReceive(timer, perform: { _ in
110
                        print("onReceive")
111
 
112
                        let duration =  CMTimeGetSeconds(playerItem.asset.duration)
113
 
114
 
115
 
116
                        let currentTime =  CMTimeGetSeconds(playerItem.currentTime())
117
 
118
 
119
                        let diference = duration - currentTime
120
                        if diference < 10 {
121
                            self.isCompleted = true;
122
                            self.timer.upstream.connect().cancel()
123
                            timerActive = false
124
                        }
125
 
126
                    })
127
 
128
 
129
                    //.frame(width: geometry.size.width, height: geometry.size.height, alignment: /*@START_MENU_TOKEN@*/.center/*@END_MENU_TOKEN@*/)
130
 
131
 
132
            }
133
            .background(Color(colorBackgroundTopic ?? .gray))
134
            .navigationBarBackButtonHidden(true)
135
            .navigationBarTitleDisplayMode(.inline)
136
            .navigationTitle(self.slideModel.name)
137
            .toolbar {
138
                ToolbarItem(placement: .navigationBarLeading) {
139
                    Button(action: {
140
 
141
                        let dataService = DataService()
142
                        if self.isCompleted {
143
 
144
 
145
                            dataService.completeSlide(slide: slideModel)
146
                        } else {
147
                            dataService.incompleteSlide(slide: slideModel)
148
                        }
149
 
150
                        if self.timerActive {
151
                            self.timer.upstream.connect().cancel()
152
                        }
153
 
154
                        //self.goToGallery = true
155
                        self.presentationMode.wrappedValue.dismiss()
156
 
157
 
158
                    }) {
159
                        HStack {
160
                            Image(systemName: "chevron.backward")
161
                                .aspectRatio(contentMode: .fit)
162
                                .foregroundColor(Color( colorAppTextView ?? .systemBlue))
163
                                .background(Color(colorAppBackground ?? .systemBlue))
164
                        }
165
                    }
166
 
167
                }
168
            }
169
 
170
        //}
171
    }
172
 
173
 
174
 
175
 
176
}
177
 
178
 
179
struct VideoPlayerView_Previews: PreviewProvider {
180
    static var previews: some View {
181
        let slideModel = SlideModel(
182
            uuid : "S123",
183
            topicUuid : "T123",
184
            capsuleUuid : "C123",
185
            quizUuid : "",
186
            name : "Video de prueba",
187
            description : "",
188
            type: Constants.SLIDE_TYPE_VIDEO,
189
            file : "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
190
            background : "",
191
            position : 1,
192
            completed : 0)
193
        VideoPlayerView(slideModel: slideModel, position : 0)
194
    }
195
}
196