无故事王国
2023-11-01 cb119c1aa79c5c5477f876660bee7ca0cb856ace
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
    //
    //  CourseVideoDetailVC.swift
    //  WanPai
    //
    //  Created by 无故事王国 on 2023/6/16.
    //
 
import UIKit
import AVKit
import SDWebImage
import JQTools
 
class CourseVideoDetailVC: BaseVC {
    @IBOutlet weak var view_videoContainer: UIView!
 
    private var id:Int?
    private var model:ExerciseVideoModel?
    private var detailModel:VideoDetailModel?
    private var exerciseVideoDetailModel:ExerciseVideoDetailModel?
 
    @IBOutlet weak var label_name: UILabel!
    @IBOutlet weak var label_introduce: UILabel!
    @IBOutlet weak var label_studyState: UILabel!
    @IBOutlet weak var img_introduceCover: UIImageView!
    @IBOutlet weak var label_coin: UILabel!
    @IBOutlet weak var cons_imgHei: NSLayoutConstraint!
    private var timeDuration:Double = 0
 
 
    private lazy var playerVC:AVPlayerViewController = {
        let player = AVPlayerViewController()
        player.allowsPictureInPicturePlayback = true
        player.delegate = self
        return player
    }()
 
 
    init(id:Int) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
    }
 
    init(model:ExerciseVideoModel){
        super.init(nibName: nil, bundle: nil)
        self.model = model
    }
 
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
        if let id {
            try? AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: .defaultToSpeaker)
            Services.benefitsVideoDetail(id: id).subscribe(onNext: { [weak self] data in
                if let model = data.data{
                    self?.detailModel = model
                    self?.playerVC.player = AVPlayer(url: URL(string: model.courseVideo)!)
                    self?.playerVC.exitsFullScreenWhenPlaybackEnds = true
                    self?.playerVC.entersFullScreenWhenPlaybackBegins = true
                    self?.playerVC.player?.play()
 
                    self?.playerVC.player?.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: 1), queue: DispatchQueue.main) { [weak self](time) in
                        self?.timeDuration += 1
                    }
 
                    self?.label_name.text = model.name
                    self?.label_coin.text = "\(model.integral)积分"
                    self?.label_introduce.text = model.introduce
                    if model.study == 1{
                        self?.label_studyState.text = "已学习"
                        self?.label_studyState.backgroundColor = UIColor(hexStr: "#318C10").withAlphaComponent(0.29)
                        self?.label_studyState.textColor = UIColor(hexStr: "#318C10")
                    }else{
                        self?.label_studyState.text = "未学习"
                        self?.label_studyState.backgroundColor = UIColor(hexStr: "#FD9331").withAlphaComponent(0.29)
                        self?.label_studyState.textColor = Def_ThemeColor
                    }
                    self?.label_studyState.isHidden = false
 
                    self?.img_introduceCover.sd_setImage(with: URL(string: model.introductionDrawing)) {[weak self] image, error, type, url in
                        if let img = image{
                            self?.img_introduceCover.image = img
                            let radio = img.size.width / img.size.height
                            self?.cons_imgHei.constant = JQ_ScreenW / radio
                        }
                    }
                }
            }).disposed(by: disposeBag)
        }
 
        if let model{
            Services.exerciseCourseDetail(coursePackageId: model.coursePackageId, videoId: model.videoId).subscribe(onNext: {[weak self] data in
                if let model = data.data{
                    self?.exerciseVideoDetailModel = model
                    self?.label_name.text = model.packageName
                    self?.label_coin.text = "\(model.integral)积分"
                    self?.label_introduce.text = model.synopsis
                    self?.label_studyState.isHidden = false
                    if model.studyStatus == 1{
                        self?.label_studyState.text = "已学习"
                        self?.label_studyState.backgroundColor = UIColor(hexStr: "#318C10").withAlphaComponent(0.29)
                        self?.label_studyState.textColor = UIColor(hexStr: "#318C10")
                    }else{
                        self?.label_studyState.text = "未学习"
                        self?.label_studyState.backgroundColor = UIColor(hexStr: "#FD9331").withAlphaComponent(0.29)
                        self?.label_studyState.textColor = Def_ThemeColor
                    }
 
                    if let videoUrl = URL(string: model.videoURL){
                        self?.playerVC.player = AVPlayer(url: videoUrl)
                        self?.playerVC.player?.play()
 
                        self?.playerVC.player?.addPeriodicTimeObserver(forInterval: CMTimeMake(value: 1, timescale: 1), queue: DispatchQueue.main) { [weak self](time) in
                            self?.timeDuration += 1
                        }
                    }else{
                        alertError(msg: "视频链接不存在或已删除")
                    }
 
                    self?.img_introduceCover.sd_setImage(with: URL(string: model.detailedDiagram), completed: {[weak self] image, error, type, url in
                        if let img = image{
                            self?.img_introduceCover.image = img
                            let radio = img.size.width / img.size.height
                            self?.cons_imgHei.constant = JQ_ScreenW / radio
                        }
                    })
                }
            }).disposed(by: disposeBag)
        }
 
        NotificationCenter.default.addObserver(self, selector: #selector(playbackEnd), name:NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
 
    }
 
    override func setUI() {
        view_videoContainer.addSubview(playerVC.view)
        playerVC.view.frame = view_videoContainer.frame
        self.addChild(playerVC)
        playerVC.didMove(toParent: self)
        label_studyState.isHidden = true
 
    }
 
    @objc private func playbackEnd(){
        if let m = exerciseVideoDetailModel{
            if timeDuration >= CMTimeGetSeconds((playerVC.player?.currentItem?.duration)!) && m.studyStatus == 0 {
                Services.updateVideoStatus(coursePackageId: m.coursePackageId, videoId: m.videoId).subscribe(onNext: {[weak self] data in
                    alertSuccess(msg: "已获得积分")
                    self?.exerciseVideoDetailModel?.studyStatus = 1
                    self?.label_studyState.text = "已学习"
                    self?.label_studyState.backgroundColor = UIColor(hexStr: "#318C10").withAlphaComponent(0.29)
                    self?.label_studyState.textColor = UIColor(hexStr: "#318C10")
                    self?.timeDuration = 0
                    NotificationCenter.default.post(name: UpdateWelfare_Noti, object: nil)
                }).disposed(by: disposeBag)
            }
        }
 
 
        if let m = detailModel{
            if timeDuration >= CMTimeGetSeconds((playerVC.player?.currentItem?.duration)!) && m.study == 0 {
                Services.receiveAward(id: m.id).subscribe(onNext: {[weak self] data in
                    alertSuccess(msg: "已获得积分")
                    self?.detailModel?.study = 1
                    self?.label_studyState.text = "已学习"
                    self?.label_studyState.backgroundColor = UIColor(hexStr: "#318C10").withAlphaComponent(0.29)
                    self?.label_studyState.textColor = UIColor(hexStr: "#318C10")
                    self?.timeDuration = 0
                    NotificationCenter.default.post(name: UpdateWelfare_Noti, object: nil)
                }).disposed(by: disposeBag)
            }
        }
    }
}
 
 
extension CourseVideoDetailVC:AVPlayerViewControllerDelegate{
 
}