PQSelecteMusicView.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. //
  2. // PQSelecteMusic.swift
  3. // BFFramework
  4. //
  5. // Created by ak on 2021/8/4.
  6. // 功能:显示编辑界面里的音乐选择界面
  7. import Foundation
  8. class PQSelecteMusicView: UIView {
  9. // 所有分类数据
  10. var catageryDatas: [PQStuckPointMusicTagsModel] = Array<PQStuckPointMusicTagsModel>.init()
  11. // 歌单数据
  12. var musicDatas: [PQVoiceModel] = Array<PQVoiceModel>.init()
  13. // 当前页码
  14. var pageNum: Int = 0
  15. // 当前视频使用的音乐数据
  16. var currentPlayingInVideoData : PQVoiceModel?
  17. // 当前试听播放的音乐数据
  18. var currentPlayData: PQVoiceModel?
  19. // 当前播放的音频
  20. var playerItem: AVPlayerItem?
  21. // 按钮点击的回调
  22. var btnClickHandle: ((_ sender: UIButton, _ bgmData: Any?) -> Void)?
  23. // 点击播放一个歌,回调
  24. var didSelectItemHandle:((_ statue:voiceStatue) -> Void)?
  25. // 当前选择的分类
  26. var currentSelectTag:PQStuckPointMusicTagsModel?
  27. //搜索出来的歌曲要插入到热门的前面 有可能是多个
  28. var searchMusiceDatas: [PQVoiceModel] = Array<PQVoiceModel>.init()
  29. //第一次进入时自动插入的数据
  30. var firstInsertVoiceModel:PQVoiceModel?
  31. lazy var avPlayer: AVPlayer = {
  32. let avPlayer = AVPlayer()
  33. PQNotification.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem, queue: .main) { [weak self] notify in
  34. BFLog(message: "AVPlayerItemDidPlayToEndTime = \(notify)")
  35. avPlayer.seek(to: CMTime(value: CMTimeValue((self?.currentPlayData?.startTime ?? 0) * 1000), timescale: CMTimeScale(playerTimescale)))
  36. if(self?.currentPlayData?.voiceStatue == .isPlaying){
  37. self?.currentPlayData?.voiceStatue = .isPause
  38. self?.selectMusicCollection.reloadData()
  39. }
  40. }
  41. // PQNotification.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer.currentItem, queue: .main) { notify in
  42. // BFLog(message: "AVPlayerItemNewErrorLogEntry = \(notify)")
  43. // }
  44. // PQNotification.addObserver(forName: .AVPlayerItemFailedToPlayToEndTime, object: avPlayer.currentItem, queue: .main) { notify in
  45. // BFLog(message: "AVPlayerItemFailedToPlayToEndTime = \(notify)")
  46. // }
  47. // PQNotification.addObserver(forName: .AVPlayerItemPlaybackStalled, object: avPlayer.currentItem, queue: .main) { notify in
  48. // BFLog(message: "AVPlayerItemPlaybackStalled = \(notify)")
  49. // }
  50. // avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: CMTimeScale(playerTimescale)), queue: .main) { [weak self] cmTime in
  51. // BFLog(message: "addPeriodicTimeObserver = \(cmTime)")
  52. // }
  53. return avPlayer
  54. }()
  55. // 音乐分类的
  56. lazy var categoryCollection: UICollectionView = {
  57. let flowLayout = UICollectionViewFlowLayout()
  58. flowLayout.sectionInset = UIEdgeInsets.zero
  59. flowLayout.minimumLineSpacing = 0
  60. flowLayout.minimumInteritemSpacing = 0
  61. flowLayout.scrollDirection = .horizontal
  62. let categoryCollection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
  63. categoryCollection.showsVerticalScrollIndicator = false
  64. categoryCollection.showsHorizontalScrollIndicator = false
  65. categoryCollection.delegate = self
  66. categoryCollection.dataSource = self
  67. categoryCollection.backgroundColor = .clear
  68. categoryCollection.register(PQSelectMusicTagsCell.self, forCellWithReuseIdentifier: "PQSelectMusicTagsCell")
  69. categoryCollection.delaysContentTouches = false
  70. return categoryCollection
  71. }()
  72. // 每一个分类下所有歌曲
  73. lazy var selectMusicCollection: UICollectionView = {
  74. let flowLayout = UICollectionViewFlowLayout()
  75. flowLayout.sectionInset = UIEdgeInsets.zero
  76. flowLayout.minimumLineSpacing = 0
  77. flowLayout.minimumInteritemSpacing = 0
  78. flowLayout.scrollDirection = .horizontal
  79. let selectMusicCollection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
  80. selectMusicCollection.showsVerticalScrollIndicator = false
  81. selectMusicCollection.showsHorizontalScrollIndicator = false
  82. selectMusicCollection.delegate = self
  83. selectMusicCollection.dataSource = self
  84. selectMusicCollection.backgroundColor = .clear
  85. selectMusicCollection.register(PQSelectMusicCell.self, forCellWithReuseIdentifier: String(describing: PQSelectMusicCell.self))
  86. selectMusicCollection.delaysContentTouches = false
  87. selectMusicCollection.contentInset = UIEdgeInsets(top: 0, left: 17, bottom: 0, right: 0)
  88. return selectMusicCollection
  89. }()
  90. // 搜索音乐btn
  91. lazy var musicSearchBtn: UIButton = {
  92. let musicSearchBtn = UIButton(type: .custom)
  93. musicSearchBtn.setTitle("搜索", for: .normal)
  94. musicSearchBtn.setImage(UIImage().BF_Image(named: "musicSearch"), for: .normal)
  95. musicSearchBtn.setTitleColor(UIColor.hexColor(hexadecimal: "#959595"), for: .normal)
  96. musicSearchBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
  97. return musicSearchBtn
  98. }()
  99. deinit {
  100. PQNotification.removeObserver(self)
  101. PQNotification.removeObserver(self.avPlayer.currentItem as Any)
  102. avPlayer.currentItem?.removeObserver(self, forKeyPath: "status")
  103. avPlayer.currentItem?.removeObserver(self, forKeyPath: "error")
  104. avPlayer.pause()
  105. avPlayer.replaceCurrentItem(with: nil)
  106. playerItem = nil
  107. }
  108. override init(frame: CGRect) {
  109. super.init(frame: frame)
  110. addSubview(categoryCollection)
  111. addSubview(selectMusicCollection)
  112. addSubview(musicSearchBtn)
  113. }
  114. func showData(){
  115. autolayout()
  116. isHidden = false
  117. //不是每一次点击都显示数据
  118. if(musicDatas.count == 0){
  119. loadRequestTagsList()
  120. }
  121. }
  122. required init?(coder _: NSCoder) {
  123. fatalError("init(coder:) has not been implemented")
  124. }
  125. //插入数据
  126. func insertSearchMusic(model:PQVoiceModel) {
  127. for oldModel in musicDatas {
  128. oldModel.voiceStatue = .isNormal
  129. }
  130. let musicIndex = musicDatas.firstIndex(where: { (music) -> Bool in
  131. (music.musicId == model.musicId)
  132. })
  133. if(musicIndex == nil){
  134. model.voiceStatue = .isSelected
  135. searchMusiceDatas.insert(model, at: 0)
  136. //有搜索的数据
  137. if(currentSelectTag?.tagId == 425){
  138. musicDatas.insert(model, at: 0)
  139. }
  140. }else{
  141. let selectMusicData = musicDatas[musicIndex!]
  142. selectMusicData.voiceStatue = .isSelected
  143. musicDatas.remove(at: musicIndex!)
  144. musicDatas.insert(selectMusicData, at: 0)
  145. }
  146. self.currentPlayingInVideoData = model
  147. selectMusicCollection.reloadData()
  148. //划动到选择的音乐位置
  149. selectMusicCollection.scrollToItem(at: IndexPath(row: 0, section: 0), at: .centeredHorizontally, animated: true)
  150. }
  151. func autolayout() {
  152. categoryCollection.snp_removeConstraints()
  153. selectMusicCollection.snp_removeConstraints()
  154. musicSearchBtn.snp_removeConstraints()
  155. categoryCollection.snp.makeConstraints { make in
  156. make.height.equalTo(20)
  157. make.left.equalToSuperview().offset(91)
  158. make.right.equalToSuperview()
  159. make.top.equalToSuperview().offset(14)
  160. }
  161. selectMusicCollection.snp.makeConstraints { make in
  162. make.height.equalTo(131)
  163. make.left.equalToSuperview()
  164. make.right.equalToSuperview()
  165. make.top.equalToSuperview().offset(54)
  166. }
  167. musicSearchBtn.snp.makeConstraints { make in
  168. make.left.equalToSuperview().offset(12)
  169. make.top.equalToSuperview().offset(14)
  170. make.height.equalTo(20)
  171. make.width.equalTo(60)
  172. }
  173. musicSearchBtn.imagePosition(at: .left, space: 8)
  174. }
  175. /// 请求标签数据
  176. /// - Returns: <#description#>
  177. func loadRequestTagsList() {
  178. PQStuckPointViewModel.stuckPointMusicCategoryList { [weak self] tags, _, _ in
  179. if tags.count > 0 {
  180. // 请求列表数据
  181. self?.requestPageListData(isRefresh: true, tagId: tags.first?.tagId ?? 0)
  182. self?.catageryDatas = tags
  183. self?.currentSelectTag = tags[0]
  184. self?.categoryCollection.reloadData()
  185. }
  186. }
  187. }
  188. /// 请求指定分类的歌列表数据
  189. /// - Returns:
  190. func requestPageListData(isRefresh: Bool = true, isHotPage _: Bool = false, tagId: Int64) {
  191. if isRefresh {
  192. pageNum = 1
  193. musicDatas = []
  194. }
  195. PQStuckPointViewModel.stuckPointMusicPageList(tagId: tagId, pageNum: pageNum, videoCount: 0, imageCount: 0, totalDuration: 0,oldDataMusic: musicDatas) { [weak self] musicInfo, _ in
  196. BFLog(message: "请求音乐列表 pageNum\(String(describing: self?.pageNum)) tagId \(tagId) 返回条数\(musicInfo.count)")
  197. if musicInfo.count > 0 {
  198. self?.pageNum = (self?.pageNum ?? 0) + 1
  199. self?.musicDatas = self!.musicDatas + musicInfo
  200. //有搜索的数据
  201. if((self?.searchMusiceDatas.count ?? 0) > 0 && self!.musicDatas.first?.cacheTagID == 425 && self?.pageNum == 2){
  202. self?.musicDatas.insert(contentsOf: self?.searchMusiceDatas ?? Array.init(), at: 0)
  203. }
  204. if(self?.musicDatas.count ?? 0 > 0){
  205. self?.selectMusicCollection.reloadData()
  206. }else{
  207. BFLog(message: "分类歌曲数据为空!!!!")
  208. }
  209. if( self?.pageNum == 2){
  210. //歌曲列表返回到头部
  211. self?.selectMusicCollection.setContentOffset(.zero, animated: false)
  212. //第一次进入插入的歌曲,插入后置空 如果 不存在只插入一次 防止重
  213. if(self?.firstInsertVoiceModel != nil){
  214. self?.insertSearchMusic(model: (self?.firstInsertVoiceModel)!)
  215. self?.currentPlayingInVideoData = self?.firstInsertVoiceModel
  216. self?.firstInsertVoiceModel = nil
  217. }
  218. }
  219. }
  220. }
  221. }
  222. /// 播放音乐
  223. /// - Parameter itemData: <#itemData description#>
  224. func playStuckPointMusic(itemData: PQVoiceModel?, isClearCurrentMusic: Bool = false) {
  225. if itemData != nil, currentPlayData != itemData {
  226. if !isValidURL(url: itemData?.musicPath ?? "") {
  227. cShowHUB(superView: nil, msg: "本歌曲暂无伴奏版本哦~")
  228. return
  229. }
  230. avPlayer.pause()
  231. playerItem?.removeObserver(self, forKeyPath: "status")
  232. playerItem?.removeObserver(self, forKeyPath: "error")
  233. if itemData!.musicId == currentPlayingInVideoData?.musicId {
  234. itemData?.voiceStatue = .isSelected
  235. currentPlayData = itemData
  236. playerItem = nil
  237. return
  238. }
  239. playerItem = AVPlayerItem(url: URL(string: itemData?.musicPath ?? "")!)
  240. if (itemData?.endTime ?? 0) > 0, (itemData?.endTime ?? 0) > (itemData?.startTime ?? 0) {
  241. playerItem?.forwardPlaybackEndTime = CMTime(value: CMTimeValue((itemData?.endTime ?? 0) * playerTimescale), timescale: CMTimeScale(playerTimescale))
  242. }
  243. avPlayer.replaceCurrentItem(with: playerItem)
  244. playerItem?.addObserver(self, forKeyPath: "status", options: .new, context: nil)
  245. playerItem?.addObserver(self, forKeyPath: "error", options: .new, context: nil)
  246. avPlayer.seek(to: CMTime(value: CMTimeValue((itemData?.startTime ?? 0) * playerTimescale), timescale: CMTimeScale(playerTimescale)))
  247. avPlayer.play()
  248. itemData?.voiceStatue = .isPlaying
  249. currentPlayData = itemData
  250. } else if itemData != nil, avPlayer.rate == 0.0 {
  251. if itemData?.musicId != currentPlayingInVideoData?.musicId {
  252. avPlayer.play()
  253. itemData?.voiceStatue = .isPlaying
  254. }
  255. } else {
  256. avPlayer.pause()
  257. itemData?.voiceStatue = .isPause
  258. }
  259. if isClearCurrentMusic {
  260. avPlayer.pause()
  261. currentPlayData = nil
  262. }
  263. }
  264. //暂停播放音乐 并刷新 UI
  265. func pausePlayer() {
  266. avPlayer.pause()
  267. if(currentPlayData?.voiceStatue == .isPlaying){
  268. currentPlayData?.voiceStatue = .isPause
  269. selectMusicCollection.reloadData()
  270. }
  271. }
  272. }
  273. extension PQSelecteMusicView {
  274. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change _: [NSKeyValueChangeKey: Any]?, context _: UnsafeMutableRawPointer?) {
  275. if object is AVPlayerItem, keyPath == "status" {
  276. BFLog(message: "AVPlayerItem - status = \((object as! AVPlayerItem).status.rawValue)")
  277. switch (object as! AVPlayerItem).status {
  278. case .unknown:
  279. break
  280. case .readyToPlay:
  281. break
  282. case .failed:
  283. break
  284. default:
  285. break
  286. }
  287. } else if object is AVPlayerItem, keyPath == "error" {
  288. BFLog(message: "AVPlayerItem - error = \(String(describing: (object as! AVPlayerItem).error))")
  289. }
  290. }
  291. }
  292. /// 卡点音乐相关代理
  293. extension PQSelecteMusicView: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate {
  294. func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  295. if collectionView == selectMusicCollection {
  296. return musicDatas.count
  297. }
  298. return catageryDatas.count
  299. }
  300. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  301. if collectionView == selectMusicCollection {
  302. let itemData: Any = musicDatas[indexPath.item]
  303. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQSelectMusicCell.self), for: indexPath) as! PQSelectMusicCell
  304. if (currentPlayingInVideoData?.musicId != nil && currentPlayingInVideoData?.musicId == (itemData as? PQVoiceModel)?.musicId){
  305. (itemData as? PQVoiceModel)?.voiceStatue = .isSelected
  306. }
  307. cell.bgmData = itemData as? PQVoiceModel
  308. cell.btnClickHandle = { [weak self] sender, bgmData in
  309. //暂停播放音乐
  310. self?.pausePlayer()
  311. self?.currentPlayingInVideoData?.voiceStatue = .isNormal
  312. self?.currentPlayingInVideoData = bgmData as? PQVoiceModel
  313. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_musicSelect, pageSource: .sp_shanyinApp_main, extParams: ["musicName":(bgmData as? PQVoiceModel)?.musicName ?? "" ,"musicId":(bgmData as? PQVoiceModel)?.musicId ?? ""], remindmsg: "")
  314. if self?.btnClickHandle != nil {
  315. self?.btnClickHandle!(sender, bgmData)
  316. }
  317. let musicIndex = self?.musicDatas.firstIndex(where: { (music) -> Bool in
  318. (music.musicId == (bgmData as? PQVoiceModel)?.musicId)
  319. })
  320. self?.musicDatas[musicIndex ?? 0].voiceStatue = .isSelected
  321. self?.selectMusicCollection.reloadData()
  322. }
  323. //自动请求下一页数据
  324. if(indexPath.row == musicDatas.count - 5){
  325. // 请求这个分类的歌单
  326. requestPageListData(isRefresh: false, tagId: currentSelectTag?.tagId ?? 0)
  327. }
  328. return cell
  329. }
  330. let itemData: Any = catageryDatas[indexPath.item]
  331. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQSelectMusicTagsCell.self), for: indexPath) as! PQSelectMusicTagsCell
  332. cell.tagData = itemData as? PQStuckPointMusicTagsModel
  333. return cell
  334. }
  335. func collectionView(_ collectionView: UICollectionView, layout _: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
  336. if collectionView == selectMusicCollection {
  337. return CGSize(width: 60 + 20, height: 131)
  338. }
  339. // 音乐分类要根据文字自适应宽度
  340. let textSize = sizeWithText(text: catageryDatas[indexPath.item].tagName ?? "", font: UIFont.systemFont(ofSize: 14, weight: .regular), size: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 20.0))
  341. return CGSize(width: textSize.width + 26, height: collectionView.frame.height)
  342. }
  343. func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
  344. //歌曲列表
  345. if (collectionView == selectMusicCollection ){
  346. musicDatas.forEach { item in
  347. item.voiceStatue = .isNormal
  348. }
  349. let music = musicDatas[indexPath.item]
  350. music.voiceStatue = .isSelected
  351. // if music.musicId != currentPlayingInVideoData?.musicId{
  352. playStuckPointMusic(itemData:music)
  353. // }else{
  354. // avPlayer.pause()
  355. // }
  356. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonClick, objectType: .ot_shanyinApp_musicVideoPreview_musicCategorySelect, pageSource: .sp_shanyinApp_main, extParams: ["categoryName":currentSelectTag?.tagName ?? "","categoryId":currentSelectTag?.tagId ?? ""], remindmsg: "")
  357. // if music.musicId != currentPlayingInVideoData?.musicId{
  358. // BFLog(1, message: "预览:\(music.musicName) 当前:\(currentPlayingInVideoData?.musicName)")
  359. // }
  360. if didSelectItemHandle != nil {
  361. didSelectItemHandle!(music.voiceStatue)
  362. }
  363. selectMusicCollection.reloadData()
  364. }else{
  365. //停止分类列表的滑动。防止切换分类时 crash
  366. selectMusicCollection.setContentOffset(selectMusicCollection.contentOffset , animated: false)
  367. if !isNetConnected() {
  368. cShowHUB(superView: nil, msg: "请有网时再试")
  369. return
  370. }
  371. catageryDatas.forEach { item in
  372. item.isSelected = false
  373. }
  374. catageryDatas[indexPath.item].isSelected = true
  375. categoryCollection.reloadData()
  376. currentSelectTag = catageryDatas[indexPath.item]
  377. // 请求这个分类的歌单
  378. requestPageListData(isRefresh: true, tagId: currentSelectTag?.tagId ?? 0)
  379. }
  380. }
  381. func collectionView(_ collectionView: UICollectionView, willDisplay _: UICollectionViewCell, forItemAt indexPath: IndexPath) {
  382. if (collectionView == selectMusicCollection && musicDatas.count > indexPath.item ){
  383. let music = musicDatas[indexPath.item]
  384. PQEventTrackViewModel.baseReportUpload(businessType: .bt_buttonView, objectType: .ot_shanyinApp_musicVideoPreview_musicView, pageSource: .sp_shanyinApp_main, extParams: ["musicName":music.musicName ?? "" ,"musicId":music.musicId ?? ""], remindmsg: "")
  385. }
  386. }
  387. }
  388. // 分类 cell
  389. class PQSelectMusicTagsCell: UICollectionViewCell {
  390. lazy var titleLab: UILabel = {
  391. let titleLab = UILabel()
  392. titleLab.font = UIFont.systemFont(ofSize: 14)
  393. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  394. titleLab.textAlignment = .center
  395. titleLab.backgroundColor = PQBFConfig.shared.styleBackGroundColor
  396. return titleLab
  397. }()
  398. override init(frame: CGRect) {
  399. super.init(frame: frame)
  400. contentView.addSubview(titleLab)
  401. }
  402. required init?(coder _: NSCoder) {
  403. fatalError("init(coder:) has not been implemented")
  404. }
  405. var tagData: PQStuckPointMusicTagsModel? {
  406. didSet {
  407. addData()
  408. addLayout()
  409. }
  410. }
  411. func addData() {
  412. titleLab.text = "\(tagData?.tagName ?? "")"
  413. if tagData?.isSelected ?? false {
  414. titleLab.textColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  415. titleLab.font = UIFont.boldSystemFont(ofSize: 14)
  416. } else {
  417. titleLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  418. titleLab.font = UIFont.systemFont(ofSize: 14)
  419. }
  420. }
  421. func addLayout() {
  422. titleLab.snp.makeConstraints { make in
  423. make.size.equalToSuperview()
  424. make.left.top.equalToSuperview()
  425. }
  426. }
  427. }
  428. // 歌曲cell PQSelectMusicCell
  429. class PQSelectMusicCell: UICollectionViewCell {
  430. // 按钮点击的回调
  431. var btnClickHandle: ((_ sender: UIButton, _ bgmData: Any?) -> Void)?
  432. var contentType: stuckPointMusicContentType = .catagery
  433. lazy var audioImageView: UIImageView = {
  434. let audioImageView = UIImageView(image: UIImage().BF_Image(named: "videomk_music_default"))
  435. audioImageView.addCorner(corner: 4)
  436. audioImageView.contentMode = .scaleAspectFill
  437. return audioImageView
  438. }()
  439. lazy var imageMaskView: UIView = {
  440. let imageMaskView = UIView()
  441. imageMaskView.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue,alpha: 0.76)
  442. imageMaskView.addCorner(corner: 4)
  443. return imageMaskView
  444. }()
  445. lazy var playImageView: UIImageView = {
  446. let playImageView = UIImageView()
  447. playImageView.image = UIImage().BF_Image(named: "stuckPoint_music_pause")
  448. playImageView.contentMode = .scaleAspectFit
  449. return playImageView
  450. }()
  451. lazy var titleLab: UILabel = {
  452. let titleLab = UILabel()
  453. titleLab.font = UIFont.systemFont(ofSize: 14)
  454. titleLab.textColor = PQBFConfig.shared.styleTitleColor
  455. return titleLab
  456. }()
  457. /// 音乐歌曲名称
  458. lazy var musicNameLab: UILabel = {
  459. let musicNameLab = UILabel()
  460. musicNameLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  461. musicNameLab.font = UIFont.systemFont(ofSize: 12)
  462. musicNameLab.textAlignment = .center
  463. musicNameLab.lineBreakMode = .byTruncatingTail
  464. musicNameLab.numberOfLines = 2
  465. return musicNameLab
  466. }()
  467. // 使用按钮
  468. lazy var confirmBtn: UIButton = {
  469. let confirmBtn = UIButton(type: .custom)
  470. confirmBtn.setTitle("使用", for: .normal)
  471. confirmBtn.setTitleColor(UIColor.white, for: .normal)
  472. confirmBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
  473. confirmBtn.addTarget(self, action: #selector(confirmClick(sender:)), for: .touchUpInside)
  474. confirmBtn.addCorner(corner: 4)
  475. confirmBtn.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  476. return confirmBtn
  477. }()
  478. lazy var remindView: UIView = {
  479. let remindView = UIView()
  480. remindView.backgroundColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  481. remindView.addCorner(corner: 3)
  482. return remindView
  483. }()
  484. @objc class func stuckPointMusicContentCell(collectionView: UICollectionView, indexPath: IndexPath) -> PQStuckPointMusicContentCell {
  485. let cell: PQStuckPointMusicContentCell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: PQStuckPointMusicContentCell.self), for: indexPath) as! PQStuckPointMusicContentCell
  486. return cell
  487. }
  488. override init(frame: CGRect) {
  489. super.init(frame: frame)
  490. contentView.addSubview(audioImageView)
  491. audioImageView.addSubview(imageMaskView)
  492. audioImageView.addSubview(playImageView)
  493. contentView.addSubview(titleLab)
  494. contentView.addSubview(musicNameLab)
  495. contentView.addSubview(remindView)
  496. contentView.addSubview(confirmBtn)
  497. }
  498. required init?(coder _: NSCoder) {
  499. fatalError("init(coder:) has not been implemented")
  500. }
  501. var bgmData: PQVoiceModel? {
  502. didSet {
  503. addData()
  504. addLayout()
  505. }
  506. }
  507. func addData() {
  508. audioImageView.setNetImage(url: "\(bgmData?.avatarUrl ?? "")", placeholder: UIImage().BF_Image(named: "videomk_music_default"))
  509. if((bgmData?.musicName ?? "").count <= 4){
  510. musicNameLab.text = (bgmData?.musicName ?? "").appending("\n ")
  511. }else{
  512. musicNameLab.text = (bgmData?.musicName ?? "")
  513. }
  514. if bgmData?.voiceStatue == .isSelected{
  515. playImageView.isHidden = false
  516. imageMaskView.isHidden = false
  517. playImageView.image = UIImage().BF_Image(named: "stuckPoint_music_selected")
  518. } else if bgmData?.voiceStatue == .isPlaying{
  519. playImageView.isHidden = false
  520. imageMaskView.isHidden = false
  521. playImageView.image = nil
  522. playImageView.kf.setImage(with: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "stuckPoint_music_playing", ofType: ".gif")!))
  523. } else if bgmData?.voiceStatue == .isPause{
  524. playImageView.isHidden = false
  525. imageMaskView.isHidden = false
  526. playImageView.image = UIImage().BF_Image(named: "stuckPoint_music_pause")
  527. }else {
  528. playImageView.isHidden = true
  529. playImageView.image = nil
  530. imageMaskView.isHidden = true
  531. }
  532. // if bgmData?.voiceStatue == .isSelected{
  533. // playImageView.isHidden = false
  534. // imageMaskView.isHidden = false
  535. // if bgmData?.voiceStatue == .isPlaying {
  536. // playImageView.image = nil
  537. // playImageView.kf.setImage(with: URL(fileURLWithPath: Bundle().BF_mainbundle().path(forResource: "stuckPoint_music_playing", ofType: ".gif")!))
  538. //
  539. // } else {
  540. // playImageView.image = UIImage().BF_Image(named: "stuckPoint_music_pause")
  541. // }
  542. //
  543. // } else {
  544. // playImageView.isHidden = true
  545. // playImageView.image = nil
  546. //
  547. // imageMaskView.isHidden = true
  548. // }
  549. confirmBtn.isHidden = !(bgmData?.voiceStatue == .isPause || bgmData?.voiceStatue == .isPlaying)
  550. if(bgmData?.voiceStatue == .isSelected || bgmData?.voiceStatue == .isPause || bgmData?.voiceStatue == .isPlaying){
  551. musicNameLab.textColor = UIColor.hexColor(hexadecimal: PQBFConfig.shared.styleColor.rawValue)
  552. musicNameLab.font = UIFont.boldSystemFont(ofSize: 12)
  553. }else{
  554. musicNameLab.textColor = UIColor.hexColor(hexadecimal: "#959595")
  555. musicNameLab.font = UIFont.systemFont(ofSize: 12)
  556. }
  557. }
  558. func addLayout() {
  559. audioImageView.snp.remakeConstraints { make in
  560. make.left.top.equalToSuperview()
  561. make.width.height.equalTo(60)
  562. }
  563. imageMaskView.snp.makeConstraints { make in
  564. make.size.equalTo(audioImageView)
  565. }
  566. playImageView.snp.remakeConstraints { make in
  567. make.center.equalToSuperview()
  568. make.width.height.equalTo(24)
  569. }
  570. musicNameLab.snp.remakeConstraints { make in
  571. make.width.equalTo(60)
  572. make.height.equalTo(30)
  573. make.top.equalTo(audioImageView.snp_bottom).offset(6)
  574. }
  575. confirmBtn.snp.remakeConstraints { make in
  576. make.width.equalTo(54)
  577. make.height.equalTo(29)
  578. make.top.equalTo(musicNameLab.snp_bottom).offset(6)
  579. make.centerX.equalTo(audioImageView.snp_centerX)
  580. }
  581. audioImageView.addCorner(corner: 60 / 2)
  582. imageMaskView.addCorner(corner: 60 / 2)
  583. }
  584. @objc func confirmClick(sender: UIButton) {
  585. if btnClickHandle != nil {
  586. btnClickHandle!(confirmBtn, bgmData)
  587. }
  588. }
  589. }