PQSelecteMusicView.swift 27 KB

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