Просмотр исходного кода

播放器暂停和恢复控制

jsonwang 4 лет назад
Родитель
Сommit
fbda5651bc

+ 37 - 23
MusicVideoPlus/MusicVideoPlus/Classes/Modules/Home/Controllers/MVHomeController.swift

@@ -95,17 +95,31 @@ class MVHomeController: MVBaseController {
         }else{
             navigationController?.pushViewController(PQStuckPointMaterialController(), animated: true)
         }
-       
-    
+  
+
+    }
+  
+    @objc func enterBackground() {
+        BFLog(message: "进入到后台")
+        controlPlayrPasueOrResume(isPause: true)
+    }
 
+    @objc func willEnterForeground() {
+        BFLog(message: "进入到前台")
+        controlPlayrPasueOrResume(isPause: false)
+    }
+    
+    override func viewWillAppear(_: Bool) {
+        super.viewWillAppear(true)
+   
+        controlPlayrPasueOrResume(isPause: false)
+        
+        PQNotification.addObserver(self, selector: #selector(enterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
+        PQNotification.addObserver(self, selector: #selector(willEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
     }
     override func viewWillDisappear(_ animated: Bool) {
         super.viewWillDisappear(animated)
-
-        // 停止播放
-        let cell: MVBannerCell? = viewListView.collectionView.cellForItem(at: lastBnnerSelectIndex) as? MVBannerCell
-        cell?.pauseView.isHidden = false
-        PQSingletoVideoPlayer.shared.stopPlayer(isRemove: false)
+        controlPlayrPasueOrResume(isPause: true)
     }
     
     func getData(){
@@ -350,27 +364,13 @@ extension MVHomeController: MVBannerDataSource, MVBannerDelegate {
 
         return cell
     }
-
-    func changePlayerStatus(indexPath: IndexPath) {
-        let cell: MVBannerCell? = viewListView.collectionView.cellForItem(at: indexPath) as? MVBannerCell
-
-        if PQSingletoVideoPlayer.shared.isPlaying {
-            cell?.pauseView.isHidden = false
-            PQSingletoVideoPlayer.shared.pausePlayer()
-
-        } else {
-            PQSingletoVideoPlayer.shared.resumePlayer()
-            cell?.pauseView.isHidden = true
-        }
-    }
-
     // MARK: - GXBannerDelegate
 
     func banner(_: MVBanner, didSelectItemAt indexPath: IndexPath) {
         NSLog("didSelectItemAt %d", indexPath.row)
         lastBnnerSelectIndex = indexPath
-
-        changePlayerStatus(indexPath: indexPath)
+ 
+        controlPlayrPasueOrResume(isPause: PQSingletoVideoPlayer.shared.isPlaying)
     }
 
     func playVideo(page: Int){
@@ -408,6 +408,20 @@ extension MVHomeController: MVBannerDataSource, MVBannerDelegate {
 
     }
     
+    //控制播放器的暂停或恢复
+    func controlPlayrPasueOrResume(isPause:Bool){
+        let cell: MVBannerCell? = viewListView.collectionView.cellForItem(at: lastBnnerSelectIndex) as? MVBannerCell
+        if(isPause){
+            cell?.pauseView.isHidden = false
+            PQSingletoVideoPlayer.shared.pausePlayer()
+        }else{
+            cell?.pauseView.isHidden = true
+            PQSingletoVideoPlayer.shared.resumePlayer()
+        }
+      
+
+    }
+    
     func pageControl(currentPage page: Int) {
         BFLog(message: "page is \(page)")
         playVideo(page: page)