//
|
// 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
|
private var needPopRoot = false
|
private var isFromFist = false
|
|
override func viewDidDisappear(_ animated: Bool) {
|
super.viewDidDisappear(animated)
|
if audioPlayer.meditationModel != nil{
|
// audioPlayer.masterPlayer?.play()
|
// audioPlayer.bgmPlayer?.play()
|
}else{
|
audioPlayer.playScene()
|
}
|
}
|
|
override func viewDidAppear(_ animated: Bool) {
|
super.viewDidAppear(animated)
|
audioPlayer.pauseScene()
|
if audioPlayer.meditationModel != nil{
|
audioPlayer.masterPlayer?.pause()
|
audioPlayer.bgmPlayer?.pause()
|
PayMusicVC.updateStatus(.pause)
|
}
|
}
|
|
init(isFromFist:Bool){
|
super.init(nibName: nil, bundle: nil)
|
self.isFromFist = isFromFist
|
}
|
|
required init?(coder: NSCoder) {
|
fatalError("init(coder:) has not been implemented")
|
}
|
|
override func viewDidLoad() {
|
super.viewDidLoad()
|
title = "泉疗愈"
|
|
yy_popBlock = { [weak self] in
|
if self?.needPopRoot ?? false{
|
if JQ_currentNavigationController().viewControllers.first is PlanGuideVC{
|
NotificationCenter.default.post(name: PlantGuideQuit_Noti, object: nil, userInfo: nil)
|
}else{
|
self?.navigationController?.popViewController(animated: true)
|
}
|
}else{
|
self?.navigationController?.popViewController(animated: true)
|
}
|
}
|
|
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!)
|
alertSuccess(msg: "设置成功")
|
|
if isFromFist{
|
NotificationCenter.default.post(name: PlantGuideQuit_Noti, object: items[index.row])
|
return
|
}
|
|
needPopRoot = true
|
NotificationCenter.default.post(name: SetBGMSuccess_Noti, object: items[index.row])
|
|
if !(UserViewModel.getLoginInfo()?.accessToken.isEmpty ?? true){
|
DispatchQueue.main.asyncAfter(delay: 0.6) {
|
NotificationCenter.default.post(name: PlantGuideQuit_Noti, object: self.items[index.row])
|
}
|
}else{
|
DispatchQueue.main.asyncAfter(delay: 0.6) {
|
self.navigationController?.popViewController()
|
|
let miniProgram = WXLaunchMiniProgramReq.object()
|
#if DEBUG
|
miniProgram.miniProgramType = .preview
|
#else
|
miniProgram.miniProgramType = .release
|
#endif
|
miniProgram.path = ""
|
WXApi.send(miniProgram){ s in
|
if !s{
|
alert(msg: "打开失败")
|
}
|
}
|
}
|
}
|
}
|
}
|
|
@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.jq_urlEncoded()) 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)
|
}
|
}
|