harry 3 år sedan
förälder
incheckning
4972f7466f

+ 12 - 50
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenCameraManager.swift

@@ -52,7 +52,6 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
     // timer循环每次录制增长的时长
     var increaseTime: CMTime = .zero
 
-    var currPlayTime: CMTime = .zero
     fileprivate var timerr: Timer?
 
     var writerlock = false
@@ -77,35 +76,13 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
     }()
 
     var groupCount = 0
-
-    var avplayerTimeObserver: NSKeyValueObservation?
     
+    // 录音文件是否正在播放
     var audioPlayerIsPlaying:Bool = false
 
     lazy var avplayer: AVPlayer = {
         let p = AVPlayer(playerItem: nil)
-        avplayerTimeObserver = p.addPeriodicTimeObserver(forInterval: CMTime(value: 1, timescale: 100), queue: DispatchQueue.global(), using: { [weak self] currTime in
-
-            guard let wself = self else { return }
-
-            // 拖动的跳跃,不要修改时间点
-            if wself.dele?.isNormalPlaying ?? false {
-//                wself.currentAssetProgress = CMTime(seconds: (currTime + wself.currPlayTime).seconds, preferredTimescale: 1000)
-
-                BFLog(message: "当前播放器状态: \(wself.avplayer.timeControlStatus.rawValue) \(wself.currentAssetProgress.seconds)")
-            }
-//            BFLog(1, message: "拖动 currTime: \(currTime.seconds)")
-            DispatchQueue.main.async { [weak self] in
-                guard let wself = self else { return }
-                // add by ak 播放状态才进行刷新 UI
-                if wself.avplayer.timeControlStatus == .playing {
-                    wself.dele?.updateSubtitle(time: wself.currentAssetProgress)
-                    wself.locationTo(time: wself.currentAssetProgress)
-                }
-            }
-
-        }) as? NSKeyValueObservation
-
+        
         return p
     }()
 
@@ -129,13 +106,6 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
 
     // MARK: 重放
 
-    deinit {
-        if avplayerTimeObserver != nil {
-            avplayer.removeTimeObserver(avplayerTimeObserver as Any)
-            avplayerTimeObserver?.invalidate()
-        }
-    }
-
     override func resetEnv() {
         guard let camera = camera else {
             cShowHUB(superView: nil, msg: "option_fail_camera".BFLocale)
@@ -449,7 +419,6 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
         // 100ms以内的重头开始播放
         if (recordItem?.materialDuraion.seconds)! - currentAssetProgress.seconds < 0.1 {
             currentAssetProgress = .zero
-            currPlayTime = .zero
             locationTo(time: currentAssetProgress)
             avplayer.replaceCurrentItem(with: nil)
         }
@@ -495,7 +464,7 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
             BFAudioPlayer.shared.configPlayer(audioPathURL: URL(fileURLWithPath: mod.wavFilePath))
             
             let tt = currentAssetProgress - mod.startCMTime
-            BFLog(1, message: "record - tt: \(tt.seconds), curr:\(currentAssetProgress.seconds) \(needPlay) mod.wavFilePath\(mod.wavFilePath ?? "")")
+            BFLog(1, message: "record - tt: \(tt.seconds), curr:\(currentAssetProgress.seconds) \(needPlay)")
             if CMTimeCompare(tt, .zero) == 0 {
                 if needPlay {
                     audioPlayerIsPlaying = true
@@ -512,16 +481,6 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
         }
     }
 
-    func findNextModel() {
-        if let mod = recordItem?.videoStickers.first(where: { mod in
-            CMTimeCompare(mod.timelineCMIn, currentAssetProgress + CMTime(seconds: 0.6, preferredTimescale: 1000)) <= 0 && CMTimeCompare((currentAssetProgress + CMTime(seconds: 0.6, preferredTimescale: 1000)), mod.timelineCMOut) < 0
-        }) {
-            currPlayTime = mod.timelineCMIn
-            currentAssetProgress = currPlayTime
-            BFLog(1, message: "视频查找下一个:\(currentAssetProgress.seconds)")
-        }
-    }
-
     func prepareToPlayNext(needPlay: Bool = true) {
         
         if let mod = recordItem?.videoStickers.first(where: { mode in
@@ -533,15 +492,18 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
             if !su {
                 if avplayer.timeControlStatus == .paused {
                     avplayer.play()
+                    setCoverImage(currentAssetProgress)
+
                 }
                 return
             }
             
-            currPlayTime = mod.timelineCMIn
-            locationTo(time: currentAssetProgress)
+            setCoverImage(currentAssetProgress)
+
+//            locationTo(time: currentAssetProgress)
             avplayerReplaceItem(newItem: AVPlayerItem(url: URL(fileURLWithPath: mod.locationPath)))
             BFLog(1, message: "当前:\(currentAssetProgress.seconds), mod:\(mod.timelineCMIn.seconds) ~ \(mod.timelineCMOut.seconds)")
-            let tt = currentAssetProgress - currPlayTime
+            let tt = currentAssetProgress - mod.timelineCMIn
             BFLog(1, message: "asset - tt: \(tt.seconds), curr:\(currentAssetProgress.seconds)")
             
             if CMTimeCompare(tt, (avplayer.currentItem?.duration ?? .zero)) == 0 {
@@ -590,7 +552,7 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
             // 如果离结束大于0.7s,代表还有下一段视频
             if ((wself.recordItem?.materialDuraion ?? .zero) - wself.currentAssetProgress).seconds > 0.7 {
 //                wself.findNextModel()
-                wself.setCoverImage(wself.currentAssetProgress)
+//                wself.setCoverImage(wself.currentAssetProgress)
 
 //                wself.prepareToPlayNext()
             } else {
@@ -623,8 +585,8 @@ class BFRecordScreenCameraManager: BFRecordScreenBaseManager {
             CMTimeCompare(m.timelineCMIn, currentAssetProgress) <= 0 && CMTimeCompare(m.timelineCMOut, currentAssetProgress) > 0
         }), (CMTimeCompare(currentAssetProgress, (recordItem?.materialDuraion ?? .zero)) < 0) {
             BFLog(1, message: "drag end:\(currentAssetProgress.seconds), mod:\(mod.timelineCMIn)")
-            currPlayTime = mod.timelineCMIn
-            playerCoverIV.image = getThumbImageAtTime(urlAsset: AVURLAsset(url: URL(fileURLWithPath: mod.locationPath)), time: currentAssetProgress - currPlayTime)
+
+            playerCoverIV.image = getThumbImageAtTime(urlAsset: AVURLAsset(url: URL(fileURLWithPath: mod.locationPath)), time: currentAssetProgress - mod.timelineCMIn)
             playerCoverIV.isHidden = false
 
             avplayerReplaceItem(newItem: AVPlayerItem(url: URL(fileURLWithPath: mod.locationPath)))