Browse Source

处理 搜索出来的音乐使用后 crash 问题

jsonwang 3 years ago
parent
commit
a79b5e07be

+ 20 - 13
BFFramework/Classes/Stuckpoint/Controller/PQEditMusicSearchController.swift

@@ -28,18 +28,18 @@ class PQEditMusicSearchController: PQBaseViewController {
 //            avPlayer.play()
             self?.playStuckPointMusic(itemData: nil)
         }
-        PQNotification.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer.currentItem, queue: .main) { notify in
-            BFLog(message: "AVPlayerItemNewErrorLogEntry = \(notify)")
-        }
-        PQNotification.addObserver(forName: .AVPlayerItemFailedToPlayToEndTime, object: avPlayer.currentItem, queue: .main) { notify in
-            BFLog(message: "AVPlayerItemFailedToPlayToEndTime = \(notify)")
-        }
-        PQNotification.addObserver(forName: .AVPlayerItemPlaybackStalled, object: avPlayer.currentItem, queue: .main) { notify in
-            BFLog(message: "AVPlayerItemPlaybackStalled = \(notify)")
-        }
-        avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: CMTimeScale(playerTimescale)), queue: .main) { [weak self] cmTime in
-            BFLog(message: "addPeriodicTimeObserver = \(cmTime)")
-        }
+//        PQNotification.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer.currentItem, queue: .main) { notify in
+//            BFLog(message: "AVPlayerItemNewErrorLogEntry = \(notify)")
+//        }
+//        PQNotification.addObserver(forName: .AVPlayerItemFailedToPlayToEndTime, object: avPlayer.currentItem, queue: .main) { notify in
+//            BFLog(message: "AVPlayerItemFailedToPlayToEndTime = \(notify)")
+//        }
+//        PQNotification.addObserver(forName: .AVPlayerItemPlaybackStalled, object: avPlayer.currentItem, queue: .main) { notify in
+//            BFLog(message: "AVPlayerItemPlaybackStalled = \(notify)")
+//        }
+//        avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: CMTimeScale(playerTimescale)), queue: .main) { [weak self] cmTime in
+//            BFLog(message: "addPeriodicTimeObserver = \(cmTime)")
+//        }
         return avPlayer
     }()
     // 输入框清空按钮
@@ -116,7 +116,14 @@ class PQEditMusicSearchController: PQBaseViewController {
         super.backBtnClick()
         avPlayer.pause()
     }
-    
+    deinit {
+        PQNotification.removeObserver(self)
+        avPlayer.currentItem?.removeObserver(self, forKeyPath: "status")
+        avPlayer.currentItem?.removeObserver(self, forKeyPath: "error")
+        avPlayer.pause()
+        avPlayer.replaceCurrentItem(with: nil)
+        playerItem = nil
+    }
     override func viewWillAppear(_ animated: Bool) {
         super.viewDidAppear(animated)
         PQEventTrackViewModel.baseReportUpload(businessType: .bt_windowView, objectType: .ot_shanyinApp_musicVideoPreview_musicSearch, pageSource: .sp_shanyinApp_main, extParams: nil, remindmsg: "")

+ 29 - 20
BFFramework/Classes/Stuckpoint/View/PQSelecteMusicView.swift

@@ -34,26 +34,26 @@ class PQSelecteMusicView: UIView {
  
     lazy var avPlayer: AVPlayer = {
         let avPlayer = AVPlayer()
-        PQNotification.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem, queue: .main) { [weak self] notify in
-            BFLog(message: "AVPlayerItemDidPlayToEndTime = \(notify)")
-            avPlayer.seek(to: CMTime(value: CMTimeValue((self?.currentPlayData?.startTime ?? 0) * 1000), timescale: CMTimeScale(playerTimescale)))
- 
-            self?.currentPlayData?.voiceStatue = .isPause
-
-            self?.selectMusicCollection.reloadData()
-        }
-        PQNotification.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer.currentItem, queue: .main) { notify in
-            BFLog(message: "AVPlayerItemNewErrorLogEntry = \(notify)")
-        }
-        PQNotification.addObserver(forName: .AVPlayerItemFailedToPlayToEndTime, object: avPlayer.currentItem, queue: .main) { notify in
-            BFLog(message: "AVPlayerItemFailedToPlayToEndTime = \(notify)")
-        }
-        PQNotification.addObserver(forName: .AVPlayerItemPlaybackStalled, object: avPlayer.currentItem, queue: .main) { notify in
-            BFLog(message: "AVPlayerItemPlaybackStalled = \(notify)")
-        }
-        avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: CMTimeScale(playerTimescale)), queue: .main) { [weak self] cmTime in
-            BFLog(message: "addPeriodicTimeObserver = \(cmTime)")
-        }
+//        PQNotification.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: avPlayer.currentItem, queue: .main) { [weak self] notify in
+//            BFLog(message: "AVPlayerItemDidPlayToEndTime = \(notify)")
+//            avPlayer.seek(to: CMTime(value: CMTimeValue((self?.currentPlayData?.startTime ?? 0) * 1000), timescale: CMTimeScale(playerTimescale)))
+// 
+//            self?.currentPlayData?.voiceStatue = .isPause
+//
+//            self?.selectMusicCollection.reloadData()
+//        }
+//        PQNotification.addObserver(forName: .AVPlayerItemNewErrorLogEntry, object: avPlayer.currentItem, queue: .main) { notify in
+//            BFLog(message: "AVPlayerItemNewErrorLogEntry = \(notify)")
+//        }
+//        PQNotification.addObserver(forName: .AVPlayerItemFailedToPlayToEndTime, object: avPlayer.currentItem, queue: .main) { notify in
+//            BFLog(message: "AVPlayerItemFailedToPlayToEndTime = \(notify)")
+//        }
+//        PQNotification.addObserver(forName: .AVPlayerItemPlaybackStalled, object: avPlayer.currentItem, queue: .main) { notify in
+//            BFLog(message: "AVPlayerItemPlaybackStalled = \(notify)")
+//        }
+//        avPlayer.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: CMTimeScale(playerTimescale)), queue: .main) { [weak self] cmTime in
+//            BFLog(message: "addPeriodicTimeObserver = \(cmTime)")
+//        }
         return avPlayer
     }()
     
@@ -106,6 +106,15 @@ class PQSelecteMusicView: UIView {
 
         return musicSearchBtn
     }()
+    
+    deinit {
+        PQNotification.removeObserver(self)
+        avPlayer.currentItem?.removeObserver(self, forKeyPath: "status")
+        avPlayer.currentItem?.removeObserver(self, forKeyPath: "error")
+        avPlayer.pause()
+        avPlayer.replaceCurrentItem(with: nil)
+        playerItem = nil
+    }
 
     override init(frame: CGRect) {
         super.init(frame: frame)