fix
杨锴
2024-09-09 677497cbfbf159417f2b1bb64aee0196c9fa6382
XQMuse/Root/Home/VC/BackgroundVoiceVC.swift
@@ -13,11 +13,52 @@
            @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.playBGM()
            }
            override func viewDidAppear(_ animated: Bool) {
                        super.viewDidAppear(animated)
                        audioPlayer.pauseBGM()
            }
            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() {
@@ -39,19 +80,79 @@
                        collectionView.bounces = false
                        collectionView.showsHorizontalScrollIndicator = false
                        collectionView.register(UINib(nibName: "HomeRelaxVoiceCCell", bundle: nil), forCellWithReuseIdentifier: "_HomeRelaxVoiceCCell")
                        collectionView.scrollToItem(at: IndexPath(row: 2, section: 0), at: .centeredHorizontally, animated: true)
                        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.playBGMAt(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 5
                        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
            }
}