瀏覽代碼

延迟播放解决 带缓存进入 APP 卡住问题

jsonwang 3 年之前
父節點
當前提交
1d73ef64e4
共有 1 個文件被更改,包括 41 次插入39 次删除
  1. 41 39
      MusicVideoPlus/Classes/Modules/Home/Controllers/MVHomeController.swift

+ 41 - 39
MusicVideoPlus/Classes/Modules/Home/Controllers/MVHomeController.swift

@@ -439,43 +439,47 @@ extension MVHomeController {
     /// 播放指定位置的视频
     /// - Parameter page: 视频数据位置
     func playVideo(page: Int) {
-        
-        if !(UIViewController.getCurrentViewController()?.isMember(of: MVHomeController.self))! {
-            BFLog(message: "当前显示的界面不是在首界面,不进行播放")
-            return
-        }
- 
-        DispatchQueue.main.async {
-            let cell: MVBannerCell? = self.viewListView.collectionView.cellForItem(at: IndexPath(row: page, section: 0)) as? MVBannerCell
-            let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
-            if self.mVideos.count > 0, cell != nil, showProtocal != nil, showProtocal == "1" {
-                cell?.pauseView.isHidden = true
-                cell?.progressView.progress = 0
-                let videoInfo = self.mAllVideos[page]
-                videoInfo.playProgress = 0
-        
-                let hotVideoIndex = self.mVideos.firstIndex(where: { (items) -> Bool in
-                    items.categoryName == videoInfo.categoryName
-                })
-
-                BFLog(message: "播放的视频分类为:\(String(describing: self.mVideos[hotVideoIndex ?? 0].categoryName)) hotVideoIndex :\(String(describing: hotVideoIndex)) page is \(page)")
-                // 设置选中状态
-                for hot in self.mVideos {
-                    hot.isSelected = false
+        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.05) {
+            if !(UIViewController.getCurrentViewController()?.isMember(of: MVHomeController.self))! {
+                BFLog(message: "当前显示的界面不是在首界面,不进行播放")
+                return
+            }
+     
+            DispatchQueue.main.async {
+                let cell: MVBannerCell? = self.viewListView.collectionView.cellForItem(at: IndexPath(row: page, section: 0)) as? MVBannerCell
+                let showProtocal: String? = getUserDefaults(key: cShowProtocal) as? String
+                if self.mVideos.count > 0, cell != nil, showProtocal != nil, showProtocal == "1" {
+                    cell?.pauseView.isHidden = true
+                    cell?.progressView.progress = 0
+                    let videoInfo = self.mAllVideos[page]
+                    videoInfo.playProgress = 0
+            
+                    let hotVideoIndex = self.mVideos.firstIndex(where: { (items) -> Bool in
+                        items.categoryName == videoInfo.categoryName
+                    })
+
+                    BFLog(message: "播放的视频分类为:\(String(describing: self.mVideos[hotVideoIndex ?? 0].categoryName)) hotVideoIndex :\(String(describing: hotVideoIndex)) page is \(page)")
+                    // 设置选中状态
+                    for hot in self.mVideos {
+                        hot.isSelected = false
+                    }
+                    self.mVideos[hotVideoIndex ?? 0].isSelected = true
+
+                    self.categoryCollectionView.reloadData()
+                    self.categoryCollectionView.scrollToItem(at: IndexPath(row: hotVideoIndex ?? 0, section: 0), at: .centeredHorizontally, animated: true)
+
+                    self.lastBnnerSelectIndex = IndexPath(row: page, section: 0)
+                   
+    //                DispatchQueue.global().async {
+                     PQSingletoVideoPlayer.shared.configPlyer(videoData: videoInfo, controllerView: cell!.converView)
+                       
+    //                }
+                    PQSingletoVideoPlayer.shared.startPlayr()
+           
+
+                } else {
+                    BFLog(message: "播放不成功: cell:\(String(describing: cell)) mVideos:\(self.mVideos.count) showProtocal: is \(String(describing: showProtocal))")
                 }
-                self.mVideos[hotVideoIndex ?? 0].isSelected = true
-
-                self.categoryCollectionView.reloadData()
-                self.categoryCollectionView.scrollToItem(at: IndexPath(row: hotVideoIndex ?? 0, section: 0), at: .centeredHorizontally, animated: true)
-
-                self.lastBnnerSelectIndex = IndexPath(row: page, section: 0)
-                
-                PQSingletoVideoPlayer.shared.configPlyer(videoData: videoInfo, controllerView: cell!.converView)
-                PQSingletoVideoPlayer.shared.startPlayr()
-        
-
-            } else {
-                BFLog(message: "播放不成功: cell:\(String(describing: cell)) mVideos:\(self.mVideos.count) showProtocal: is \(String(describing: showProtocal))")
             }
         }
     }
@@ -576,9 +580,7 @@ extension MVHomeController: UICollectionViewDelegate, UICollectionViewDataSource
         viewListView.currentIndex = videosCount
         viewListView.scrollToItem(at: videosCount, animated: false)
 
-        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 0.05) {
-            self.playVideo(page: videosCount)
-        }
+        self.playVideo(page: videosCount)
     }
 }