杨锴
2024-09-12 e15c976316feef72ff9bcabce38e0a078f9505db
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
//
//  BackgroundVoiceVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/14.
//
 
import UIKit
import QMUIKit
import JQTools
 
class BackgroundVoiceVC: BaseVC {
 
                @IBOutlet weak var slider_voice: UISlider!
                @IBOutlet weak var collectionView: UICollectionView!
 
                private var items = [BGMModel]()
                private var settingModel = UserDefaultSettingViewModel.getSetting()
                private var audioPlayer = AudioPlayer.getSharedInstance()
                private var playAtIndex:IndexPath?
                private var tempPlayer:AVPlayer?
                private var isPlaying:Bool = false
 
                override func viewDidDisappear(_ animated: Bool) {
                                super.viewDidDisappear(animated)
                                audioPlayer.playScene()
                }
 
                override func viewDidAppear(_ animated: Bool) {
                                super.viewDidAppear(animated)
                                audioPlayer.pauseScene()
                }
 
                override func viewDidLoad() {
        super.viewDidLoad()
                                title = "心泉·疗愈"
 
                                slider_voice.isEnabled = false
                                slider_voice.value = Float(settingModel?.volume ?? 0.5)
                                slider_voice.addTarget(self, action: #selector(volumeChange(_:)), for: .valueChanged)
                                Services.getBGM().subscribe(onNext: {[unowned self]data in
                                                if let m = data.data{
                                                                self.items = m
                                                                self.slider_voice.isEnabled = true
                                                                self.collectionView.reloadData()
 
                                                                DispatchQueue.main.asyncAfter(delay: 0.8) {
                                                                                for (index,v) in m.enumerated(){
                                                                                                if v.id == self.settingModel?.bgm?.id{
                                                                                                                self.playAtIndex = IndexPath(row: index, section: 0)
                                                                                                                self.collectionView.scrollToItem(at: IndexPath(row: index, section: 0), at: .centeredHorizontally, animated: true)
                                                                                                                break
                                                                                                }
                                                                                }
                                                                                self.collectionView.reloadData()
                                                                }
                                                }
                                }).disposed(by: disposeBag)
 
                                //播放完成
                                NotificationCenter.default.addObserver(self, selector: #selector(playBGMbackEnd), name:NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
    }
 
                override func setUI() {
 
                                let v1 = UIView()
                                v1.backgroundColor = UIColor(hexStr: "#B6DC90")
                                v1.bounds = CGRect(x: 0, y: 0, width: 17, height: 17)
                                v1.jq_borderWidth = 2
                                v1.jq_borderColor = .white
                                v1.jq_cornerRadius = 8.5
 
                                slider_voice.setThumbImage(v1.qmui_snapshotLayerImage(), for: .normal)
                                slider_voice.setThumbImage(v1.qmui_snapshotLayerImage(), for: .highlighted)
 
                                collectionView.collectionViewLayout = TestLeftRightCollectionViewFlowLayout(width: JQ_ScreenW, height: 213.5)
                                collectionView.delegate = self
                                collectionView.dataSource = self
                                collectionView.isPagingEnabled = false
                                collectionView.bounces = false
                                collectionView.showsHorizontalScrollIndicator = false
                                collectionView.register(UINib(nibName: "HomeRelaxVoiceCCell", bundle: nil), forCellWithReuseIdentifier: "_HomeRelaxVoiceCCell")
                                collectionView.contentInset = UIEdgeInsets(top: 0, left: 100 , bottom: 0, right: 100)
                }
 
                @objc func playBGMbackEnd(){
                                isPlaying = false
                                collectionView.reloadData()
                }
 
                @IBAction func setttingAction(_ sender: UIButton) {
                                if settingModel != nil{
                                                guard let index = playAtIndex else {return}
                                                settingModel?.bgm = items[index.row]
                                                settingModel?.volume = Double(slider_voice.value)
                                                UserDefaultSettingViewModel.saveSetting(settingModel!)
                                                audioPlayer.playSceneAt(items[index.row].audioFile)
                                                alertSuccess(msg: "设置成功")
                                                NotificationCenter.default.post(name: SetBGMSuccess_Noti, object: items[index.row])
                                }
                }
 
                @objc func volumeChange(_ slider:UISlider){
                                tempPlayer?.volume = slider.value
                }
 
                deinit{
                                NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
                }
}
 
extension BackgroundVoiceVC:UIScrollViewDelegate{
 
                func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
                                let p = CGPoint(x: collectionView.contentOffset.x + collectionView.frame.size.width / 2, y:0)
                                if let v = collectionView.indexPathForItem(at: p){
                                                playAtIndex = v
                                }
                }
}
 
extension BackgroundVoiceVC:UICollectionViewDelegate & UICollectionViewDataSource{
 
                func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
                                playAtIndex = indexPath
                                let model = items[indexPath.row]
                                collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: true)
 
                                guard let URL = URL(string: model.audioFile) else { return }
 
                                tempPlayer = AVPlayer(url: URL)
                                tempPlayer?.volume = slider_voice.value
                                tempPlayer!.play()
                                isPlaying = true
                                collectionView.reloadData()
                }
 
                func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
                                return items.count
                }
 
                func numberOfSections(in collectionView: UICollectionView) -> Int {
                                return 1
                }
 
                func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
                                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "_HomeRelaxVoiceCCell", for: indexPath) as! HomeRelaxVoiceCCell
                                cell.contentView.backgroundColor = .jq_randomColor
                                cell.setModel(items[indexPath.row])
 
                                if isPlaying && indexPath == playAtIndex{
                                                cell.img_play.image = UIImage(named: "icon_play_purse")
                                }else{
                                                cell.img_play.image = UIImage(named: "icon_play")
                                }
                                return cell
                }
}
 
extension BackgroundVoiceVC:UICollectionViewDelegateFlowLayout{
                func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
                                return CGSize(width: 148.25, height: 213.45)
                }
}