Browse Source

准备定时器驱动

胡志强 3 years ago
parent
commit
360552362b

+ 25 - 0
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenBaseManager.swift

@@ -39,6 +39,7 @@ class BFRecordScreenBaseManager : NSObject{
     }
 
     var recording = false
+    var isPlaying = false
     // MARK: -
     func resetEnv(){
         progreddL?.text = CMTimeGetSeconds(currentAssetProgress).formatDurationToHMS()
@@ -92,6 +93,30 @@ class BFRecordScreenBaseManager : NSObject{
         }
     }
     
+    func checkCurrentPlayStatus(){
+        if let voice = recordItem?.getUsedVoices().first(where: { mod in
+            (mod.startCMTime - currentAssetProgress).seconds <= 0.05 && (mod.endCMTime - currentAssetProgress).seconds > 0.05
+        }){
+            let su = (recordPlayer?.currentItem?.asset as? AVURLAsset)?.url.lastPathComponent != (voice.wavFilePath as NSString?)?.lastPathComponent
+            if (recordPlayer?.currentItem == nil) || su || (!su && !isPlaying){
+                if su { // 我需要换源地址
+                    recordPlayer?.pause()
+                    recordPlayer?.replaceCurrentItem(with: AVPlayerItem(url: URL(fileURLWithPath: voice.wavFilePath)))
+                }
+                isPlaying = true
+                let tt = max(CMTime.zero, currentAssetProgress - voice.startCMTime)
+                BFLog(1, message: "tt:\(tt.seconds)")
+                recordPlayer?.seek(to: tt , toleranceBefore: CMTime(seconds: 1, preferredTimescale: 10000), toleranceAfter: CMTime(seconds: 1, preferredTimescale: 10000), completionHandler: {[weak self] isfinished in
+                    guard let wself = self else { return }
+                    
+                    wself.recordPlayer?.play()
+
+                })
+            }
+            
+        }
+    }
+    
     deinit {
         clean()
         BFLog(1, message: "\(self) release")

+ 60 - 1
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenCameraManager.swift

@@ -172,8 +172,8 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
             DispatchQueue.main.asyncAfter(deadline: .now() + 0.3, execute: { [weak self] in
                 guard let wself = self else { return }
                 while wself.groupCount > 0{
-                    wself.gropQueue.leave()
                     wself.groupCount -= 1
+                    wself.gropQueue.leave()
                 }
 
                 wself.endRecord()
@@ -284,6 +284,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
             }else {
                 wself.recordEndCallBack?(false, nil)
                 wself.videoModel.locationPath = "nil"
+                wself.cameraProgressV?.newRecordLine = true
             }
         }
     }
@@ -323,7 +324,9 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
     
     override func recordFinished(isSuccess:Bool) {
         recordFinishedResult = isSuccess
+        groupCount = groupCount - 1
         gropQueue.leave()
+
     }
 
     override func deleteRecord(at: CMTime) {
@@ -711,6 +714,62 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
     }
     
     // MARK: - 私有方法
+    
+    /// 为播放/录制启动定时器
+    /// - Parameter isPlay: 是播放么? true为播放,false为录制
+    func startTimer(isPlay: Bool) {
+                
+        let startPlayTime = Date()
+        let startProgress = currentAssetProgress
+        
+        timerr?.invalidate()
+        timerr = Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true, block: {[weak self] _ in
+            guard let wself = self else { return }
+
+            let currTime = startProgress + CMTime(seconds: Date().timeIntervalSince(startPlayTime), preferredTimescale: 1000)
+
+            if isPlay {
+                wself.timerForPlay(currTime: currTime)
+            } else {
+                wself.timerForRecord(currTime: currTime)
+            }
+            
+//            wself.getThumImage()
+        })
+        RunLoop.current.add(timerr!, forMode: .common)
+    }
+    
+    func timerForPlay(currTime:CMTime){
+        dele?.progreddL.text = String(format: "%@", max(0, min(currTime.seconds, (recordItem?.materialDuraion.seconds ?? 0))).formatDurationToHMS())
+        currentAssetProgress = min(currTime, (recordItem?.materialDuraion ?? .zero))
+        dele?.progressThumV.progress = currentAssetProgress
+        dele?.updateSubtitle(time: currentAssetProgress)
+        
+        if currTime.seconds >= (recordItem?.materialDuraion.seconds ?? 0) {
+            dele?.pause()
+            isPlaying = false
+            return
+        }
+        
+        checkCurrentPlayStatus()
+    }
+    
+    func timerForRecord(currTime:CMTime){
+        if BFRecordScreenCameraManager.initOpenCamera {
+            return
+        }
+        //MARK: 进度计时器
+
+        let currDur = CMTime(seconds: videoModel.timelineCMIn.seconds + (movieWrite?.duration.seconds ?? 0), preferredTimescale: 1000)
+        if CMTimeCompare(currDur, currentAssetProgress) > 0 {
+            increaseTime = currDur - currentAssetProgress
+            currentAssetProgress = currDur
+            videoModel.timelineCMOut = currDur
+            recordProgressCallBack?(currDur)
+            updateUI(progress: currDur)
+//                BFLog(1, message: "frame: \(currentAssetProgress.seconds)")
+        }
+    }
 
     // 定位到某个时间点,相应进度条的UI更新
     func locationTo(time:CMTime) {

+ 1 - 26
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenImageManager.swift

@@ -13,7 +13,7 @@ import BFCommonKit
 class BFRecordScreenImageManager : BFRecordScreenBaseManager{
     
     fileprivate var timerr:Timer?
-    var isPlaying = false
+
     override func resetEnv() {
         // 重置指针
         currentAssetProgress = .zero
@@ -58,31 +58,6 @@ class BFRecordScreenImageManager : BFRecordScreenBaseManager{
         RunLoop.current.add(timerr!, forMode: .common)
     }
     
-    func checkCurrentPlayStatus(){
-        if let voice = recordItem?.getUsedVoices().first(where: { mod in
-            (mod.startCMTime - currentAssetProgress).seconds <= 0.05 && (mod.endCMTime - currentAssetProgress).seconds > 0.05
-        }){
-            let su = (recordPlayer?.currentItem?.asset as? AVURLAsset)?.url.lastPathComponent != (voice.wavFilePath as NSString?)?.lastPathComponent
-            if (recordPlayer?.currentItem == nil) || su || (!su && !isPlaying){
-                if su { // 我需要换源地址
-                    recordPlayer?.pause()
-                    recordPlayer?.replaceCurrentItem(with: AVPlayerItem(url: URL(fileURLWithPath: voice.wavFilePath)))
-                }
-                isPlaying = true
-                let tt = max(CMTime.zero, currentAssetProgress - voice.startCMTime)
-                BFLog(1, message: "tt:\(tt.seconds)")
-                recordPlayer?.seek(to: tt , toleranceBefore: CMTime(seconds: 1, preferredTimescale: 10000), toleranceAfter: CMTime(seconds: 1, preferredTimescale: 10000), completionHandler: {[weak self] isfinished in
-                    guard let wself = self else { return }
-                    
-                    wself.recordPlayer?.play()
-//                    if isfinished {
-//                    }
-                })
-            }
-            
-        }
-    }
-    
     override func pause() {
         timerr?.invalidate()
         timerr = nil