Browse Source

Merge branch 'dev' of https://git.yishihui.com/iOS/BFRecordScreenKit into dev
合并代码

jsonwang 3 years ago
parent
commit
23bd1bf050

+ 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

@@ -174,8 +174,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()
@@ -286,6 +286,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
             }else {
                 wself.recordEndCallBack?(false, nil)
                 wself.videoModel.locationPath = "nil"
+                wself.cameraProgressV?.newRecordLine = true
             }
         }
     }
@@ -325,7 +326,9 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
     
     override func recordFinished(isSuccess:Bool) {
         recordFinishedResult = isSuccess
+        groupCount = groupCount - 1
         gropQueue.leave()
+
     }
 
     override func deleteRecord(at: CMTime) {
@@ -739,6 +742,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) {

+ 3 - 21
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -2301,6 +2301,7 @@ public class BFRecordScreenController: BFBaseViewController {
         }
     }
 
+    //视频模式使用
     func setAudioPlay(item: AVPlayerItem?) {
         BFLog(message: "设置播放器item:\(String(describing: item))")
         guard let item = item else {
@@ -2718,28 +2719,9 @@ public extension BFRecordScreenController {
     /// 播放录音
     /// - Parameter time: <#time description#>
     func startPlayRecord(time: CMTime) {
-        // 播放对应的录音音频
-        if currMediaType == .Image {
-            if itemModels[currItemModelIndex].materialDuraion.seconds <= 0 {
-                playBtn.isSelected = true
-                playBtn.isHidden = true
-                return
-            }
-            isNormalPlaying = true
-            // 当开始播放时重置录音播放起始时间
-            recordStartPlayTime = currentAssetProgress
-            currenStartPlayTime = CMTime.zero
-        }
-        
         // 音
-        playRecord(at: time, periodicTimeObserver: { [weak self] currentT, _ in
-            
-            guard let wself = self else { return }
-            
-            BFLog(1, message: "播放录音进度:\(currentT.seconds)")
-            if wself.currMediaType == .Image, wself.isNormalPlaying {
-                wself.imageRecordProgress(progress: CMTimeGetSeconds(currentT))
-            }
+        playRecord(at: time, periodicTimeObserver: {  _, _ in
+
         }, didPlayToEndTime: { [weak self] recordInfo, currentItem in
             
             guard let wself = self else { return }

+ 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