MVPlayViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. //
  2. // MVPlayViewController.swift
  3. // MusicVideoPlus
  4. //
  5. // Created by SanW on 2021/6/22.
  6. //
  7. import BFFramework
  8. import UIKit
  9. import WechatOpenSDK
  10. class MVPlayViewController: PQBaseViewController {
  11. var itemsList: [PQVideoListModel] = Array<PQVideoListModel>.init()
  12. var pageNum: Int = 1
  13. var currentIndex: IndexPath?
  14. var currentActionBtn: UIButton? // 记录上次点击按钮
  15. var currentVideoData: PQVideoListModel? // 记录上次点击数据
  16. var isRequesting: Bool = false // 是否正在请求数据
  17. var isPlayEnd: Bool = false // 是否播放结束
  18. lazy var leftBtn: UIButton = {
  19. let leftBtn = UIButton(type: .custom)
  20. leftBtn.frame = CGRect(x: 0, y: cDevice_iPhoneStatusBarHei, width: cDefaultMargin * 4, height: cDefaultMargin * 4)
  21. leftBtn.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: -5, right: 0)
  22. leftBtn.tintColor = UIColor.white
  23. leftBtn.setImage(UIImage().BF_Image(named: "icon_detail_back").withRenderingMode(.alwaysTemplate), for: .normal)
  24. leftBtn.addTarget(self, action: #selector(backBtnClick), for: .touchUpInside)
  25. return leftBtn
  26. }()
  27. lazy var collectionView: UICollectionView = {
  28. let layout = UICollectionViewFlowLayout()
  29. layout.minimumLineSpacing = 0
  30. layout.sectionInset = UIEdgeInsets.zero
  31. layout.minimumInteritemSpacing = 0
  32. layout.itemSize = view.frame.size
  33. let collectionView = UICollectionView(frame: view.frame, collectionViewLayout: layout)
  34. collectionView.scrollsToTop = false
  35. collectionView.isPagingEnabled = true
  36. collectionView.showsVerticalScrollIndicator = false
  37. collectionView.register(MVPlayControlViewCell.self, forCellWithReuseIdentifier: String(describing: MVPlayControlViewCell.self))
  38. collectionView.delegate = self
  39. collectionView.dataSource = self
  40. if #available(iOS 11.0, *) {
  41. collectionView.contentInsetAdjustmentBehavior = .never
  42. } else {
  43. automaticallyAdjustsScrollViewInsets = false
  44. }
  45. collectionView.addRefreshView { [weak self] isRefresh in
  46. self?.loadRequestData(isRefresh: isRefresh)
  47. }
  48. return collectionView
  49. }()
  50. override func viewDidLoad() {
  51. super.viewDidLoad()
  52. PQSingletoVideoPlayer.shared.stopPlayer()
  53. view.addSubview(collectionView)
  54. view.addSubview(leftBtn)
  55. if itemsList.count <= 0 {
  56. loadRequestData()
  57. }
  58. if currentIndex != nil, itemsList.count > (currentIndex?.item ?? 0) {
  59. collectionView.contentOffset = CGPoint(x: 0, y: CGFloat(currentIndex?.item ?? 0) * view.frame.size.height)
  60. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) { [weak self] in
  61. self?.scrollViewDidEndDecelerating(self!.collectionView)
  62. }
  63. }
  64. PQSingletoVideoPlayer.shared.playStatusBloc = { [weak self] status in
  65. if status == .PQVIDEO_PLAY_STATUS_END {
  66. self?.isPlayEnd = true
  67. if ((self?.currentIndex?.item ?? 0) + 1) < (self?.itemsList.count ?? 0) {
  68. // self?.collectionView.scrollToItem(at: IndexPath(item: (self?.currentIndex?.item ?? 0) + 1, section: 0), at: .top, animated: false)
  69. self?.collectionView.contentOffset = CGPoint(x: 0, y: CGFloat((self?.currentIndex?.item ?? 0) + 1) * (self!.collectionView.frame.height))
  70. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) { [weak self] in
  71. self?.scrollViewDidEndDecelerating(self!.collectionView)
  72. }
  73. } else {
  74. (self?.collectionView.visibleCell() as? MVPlayControlViewCell)?.tagClick()
  75. self?.loadRequestData(isRefresh: false)
  76. }
  77. if (self?.currentIndex?.item ?? 0) >= ((self?.itemsList.count ?? 0) - 2) {
  78. self?.loadRequestData(isRefresh: false)
  79. }
  80. }else if status == .PQVIDEO_PLAY_STATUS_BEGIN{
  81. self?.isPlayEnd = false
  82. }
  83. }
  84. PQSingletoVideoPlayer.shared.progressBloc = { [weak self] _, playProgress, duration in
  85. (self?.collectionView.visibleCell() as? MVPlayControlViewCell)?.updateProgress(progress: playProgress / duration)
  86. }
  87. PQNotification.addObserver(self, selector: #selector(didBecomeActiveNotification), name: UIApplication.didBecomeActiveNotification, object: nil)
  88. PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
  89. PQNotification.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
  90. }
  91. override func viewDidAppear(_ animated: Bool) {
  92. super.viewDidAppear(animated)
  93. PQSingletoVideoPlayer.shared.resumePlayer()
  94. }
  95. override func viewDidDisappear(_ animated: Bool) {
  96. super.viewDidDisappear(animated)
  97. PQSingletoVideoPlayer.shared.pausePlayer()
  98. }
  99. /// 请求网络数据
  100. /// - Parameter isRefresh: <#isRefresh description#>
  101. /// - Returns: <#description#>
  102. func loadRequestData(isRefresh: Bool = true,isAutoPlay:Bool = false) {
  103. if isRequesting {
  104. return
  105. }
  106. isRequesting = true
  107. if isRefresh {
  108. pageNum = 1
  109. } else {
  110. pageNum = pageNum + 1
  111. }
  112. MVMineViewModel.userVideoListData(pageNum: pageNum) { [weak self] videoList, _ in
  113. self?.isRequesting = false
  114. if videoList != nil, (videoList?.count ?? 0) > 0 {
  115. if isRefresh {
  116. self?.itemsList = videoList!
  117. } else {
  118. self?.itemsList = self!.itemsList + videoList!
  119. }
  120. self?.collectionView.reloadData()
  121. if self?.currentIndex == nil {
  122. self?.scrollViewDidEndDecelerating(self!.collectionView)
  123. }else if (self?.isPlayEnd ?? false), ((self?.currentIndex?.item ?? 0) + 1) < (self?.itemsList.count ?? 0) {
  124. self?.collectionView.contentOffset = CGPoint(x: 0, y: CGFloat((self?.currentIndex?.item ?? 0) + 1) * (self!.collectionView.frame.height))
  125. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.2) { [weak self] in
  126. self?.scrollViewDidEndDecelerating(self!.collectionView)
  127. }
  128. }
  129. } else {
  130. self?.pageNum = (self?.pageNum ?? 1) - 1
  131. }
  132. self?.collectionView.mj_header?.endRefreshing()
  133. self?.collectionView.mj_footer?.endRefreshing()
  134. }
  135. }
  136. deinit {
  137. PQNotification.removeObserver(self)
  138. PQSingletoVideoPlayer.shared.stopPlayer()
  139. }
  140. }
  141. extension MVPlayViewController: UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
  142. func collectionView(_: UICollectionView, numberOfItemsInSection _: Int) -> Int {
  143. return itemsList.count
  144. }
  145. func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
  146. let itemData = itemsList[indexPath.item]
  147. let cell = MVPlayControlViewCell.playControlCell(collectionView: collectionView, indexPath: indexPath)
  148. cell.videoData = itemData
  149. cell.btnClickHandle = { [weak self] sender, videoData in
  150. self?.btnClickHandle(sender: sender, videoData: videoData)
  151. }
  152. return cell
  153. }
  154. func collectionView(_: UICollectionView, didSelectItemAt _: IndexPath) {}
  155. func scrollViewDidEndDecelerating(_: UIScrollView) {
  156. if itemsList.count <= 0 {
  157. PQSingletoVideoPlayer.shared.stopPlayer()
  158. return
  159. }
  160. let cell: MVPlayControlViewCell? = collectionView.visibleCell() as? MVPlayControlViewCell
  161. if cell == nil {
  162. return
  163. }
  164. let indexPath = collectionView.indexPath(for: cell!)
  165. if currentIndex != nil, currentIndex?.item == indexPath?.item,PQSingletoVideoPlayer.shared.playControllerView != nil {
  166. // PQSingletoVideoPlayer.shared.resumePlayer()
  167. return
  168. }
  169. currentIndex = indexPath
  170. let itemData = itemsList[indexPath?.item ?? 0]
  171. itemData.playProgress = 0
  172. cell?.pauseBtn.isSelected = false
  173. PQSingletoVideoPlayer.shared.configPlyer(videoData: itemData, controllerView: cell!.coverImageView, renderMode: .RENDER_MODE_FILL_EDGE)
  174. PQSingletoVideoPlayer.shared.resetPlayer()
  175. if (indexPath?.item ?? 0) >= itemsList.count - 2 {
  176. loadRequestData(isRefresh: false)
  177. }
  178. }
  179. /// 按钮点击处理
  180. /// - Parameters:
  181. /// - sender: <#sender description#>
  182. /// - videoData: <#videoData description#>
  183. /// - Returns: <#description#>
  184. func btnClickHandle(sender: UIButton, videoData: PQVideoListModel?) {
  185. switch sender.tag {
  186. case 1: // 暂停/播放
  187. if sender.isSelected {
  188. PQSingletoVideoPlayer.shared.pausePlayer()
  189. } else {
  190. PQSingletoVideoPlayer.shared.resumePlayer()
  191. }
  192. case 2: // 昵称
  193. break
  194. case 3: // 做同款
  195. let navVc: UINavigationController? = (UIApplication.shared.keyWindow?.rootViewController) as? UINavigationController
  196. (navVc?.viewControllers.first as? MVHomeController)?.jumpToDetailVc(type: 2, videoData: videoData)
  197. case 4: // 分享好友
  198. if !PQSingletoWXApiUtil.shared.isInstallWX() {
  199. cShowHUB(superView: nil, msg: "您还未安装微信客户端!")
  200. return
  201. }
  202. cShowHUB(superView: nil, msg: nil)
  203. let shareId = getUniqueId(desc: "\(videoData?.uniqueId ?? "")shareId")
  204. PQBaseViewModel.wxFriendShareInfo(videoId: (videoData?.uniqueId)!) { [weak self] imagePath, title, shareWeappRawId, msg in
  205. if msg != nil {
  206. cShowHUB(superView: nil, msg: "网络不佳哦")
  207. return
  208. }
  209. PQSingletoWXApiUtil.shared.share(type: 3, scene: Int32(WXSceneSession.rawValue), shareWeappRawId: shareWeappRawId, title: title, description: title, imageUrl: imagePath, path: videoData?.videoPath, videoId: (videoData?.uniqueId)!, pageSource: videoData?.pageSource ?? .sp_category, shareId: shareId).wxApiUtilHander = { _, _ in
  210. self?.currentActionBtn = sender
  211. self?.currentVideoData = videoData
  212. }
  213. cHiddenHUB(superView: nil)
  214. }
  215. case 5: // 分享朋友圈
  216. if !PQSingletoWXApiUtil.shared.isInstallWX() {
  217. cShowHUB(superView: nil, msg: "您还未安装微信客户端!")
  218. return
  219. }
  220. let shareId = getUniqueId(desc: "\(videoData?.uniqueId ?? "")shareId")
  221. PQBaseViewModel.h5ShareLinkInfo(videoId: videoData?.uniqueId ?? "", pageSource: videoData?.pageSource ?? .sp_category) { [weak self] path, _ in
  222. cHiddenHUB(superView: nil)
  223. if path != nil {
  224. PQSingletoWXApiUtil.shared.share(type: 1, scene: Int32(WXSceneTimeline.rawValue), title: BFLoginUserInfo.shared.isLogin() ? "\(BFLoginUserInfo.shared.nickName)made a music video for you" : "Music Video for U", description: "", imageUrl: videoData?.shareImgPath, path: path, videoId: (videoData?.uniqueId)!, pageSource: videoData?.pageSource ?? .sp_category, shareId: shareId).wxApiUtilHander = { _, _ in
  225. self?.currentActionBtn = sender
  226. self?.currentVideoData = videoData
  227. }
  228. } else {
  229. cShowHUB(superView: nil, msg: "网络不佳哦")
  230. }
  231. }
  232. default:
  233. break
  234. }
  235. }
  236. @objc func didBecomeActiveNotification() {
  237. if currentActionBtn != nil {
  238. DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
  239. cShowHUB(superView: nil, msg: "分享成功")
  240. }
  241. if currentActionBtn?.tag == 4 {
  242. if currentVideoData?.shareCountFriend != nil, currentVideoData?.shareCountFriend ?? 0 > 0 {
  243. currentVideoData?.shareCountFriend = (currentVideoData?.shareCountFriend ?? 0) + 1
  244. } else {
  245. currentVideoData?.shareCountFriend = 1
  246. }
  247. } else if currentActionBtn?.tag == 5 {
  248. if currentVideoData?.shareCount != nil, currentVideoData?.shareCount ?? 0 > 0 {
  249. currentVideoData?.shareCount = (currentVideoData?.shareCount ?? 0) + 1
  250. } else {
  251. currentVideoData?.shareCount = 1
  252. }
  253. }
  254. currentActionBtn = nil
  255. currentVideoData = nil
  256. collectionView.reloadData()
  257. }
  258. }
  259. @objc func enterBackground() {
  260. BFLog(message: "进入到后台")
  261. let navVc: UINavigationController? = (UIApplication.shared.keyWindow?.rootViewController) as? UINavigationController
  262. if navVc?.visibleViewController is MVPlayViewController {
  263. (collectionView.visibleCell() as? MVPlayControlViewCell)?.pauseBtn.isSelected = true
  264. PQSingletoVideoPlayer.shared.stopPlayer(isRemove: false)
  265. }
  266. }
  267. @objc func willEnterForeground() {
  268. BFLog(message: "进入到前台")
  269. let navVc: UINavigationController? = (UIApplication.shared.keyWindow?.rootViewController) as? UINavigationController
  270. if navVc?.visibleViewController is MVPlayViewController {
  271. (collectionView.visibleCell() as? MVPlayControlViewCell)?.pauseBtn.isSelected = false
  272. PQSingletoVideoPlayer.shared.startPlayr()
  273. }
  274. }
  275. override open var preferredStatusBarStyle: UIStatusBarStyle {
  276. return .lightContent
  277. }
  278. }