Browse Source

1,变音播放逻辑 有什么找查不到音频数据问题。

jsonwang 3 years ago
parent
commit
4e919507cd

+ 55 - 27
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenCameraManager.swift

@@ -92,7 +92,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
             guard let wself = self else { return }
             
  
-            BFLog(message: "当前播放器状态: \(wself.avplayer.timeControlStatus.rawValue) \(wself.avplayer.currentItem?.status.rawValue)")
+//            BFLog(message: "当前播放器状态: \(wself.avplayer.timeControlStatus.rawValue) \(wself.avplayer.currentItem?.status.rawValue)")
             // 拖动的跳跃,不要修改时间点
             if (wself.dele?.isNormalPlaying ?? false){
                 wself.currentAssetProgress = CMTime(seconds: (currTime + wself.currPlayTime).seconds, preferredTimescale: 1000)
@@ -128,6 +128,8 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
         return layer
     }()
     
+    var dispatchWorkItemIsCancel:Bool = false
+    
 // MARK: 重放
     
     deinit {
@@ -459,23 +461,29 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
     }
     
     func playRecordVoice(needPlay:Bool = true){
+        
+        BFLog(message: "开始查找!!!!currentAssetProgress:\(currentAssetProgress)")
         // 播放音频
         var useVoiceStickers = [PQVoiceModel]()
         if((recordItem?.voiceChangeStickers.count ?? 0) > 0){
+
             useVoiceStickers = recordItem?.voiceChangeStickers ?? [PQVoiceModel]()
         }else{
             useVoiceStickers = recordItem?.voiceStickers ?? [PQVoiceModel]()
         }
         
         for model in useVoiceStickers {
-            BFLog(message: " 时间范围:\(model.startCMTime.seconds) 到 \(model.endCMTime.seconds) \(currentAssetProgress.seconds)")
+            BFLog(message: " 时间范围:\(model.startCMTime.seconds) 到 \(model.endCMTime.seconds) \(currentAssetProgress.seconds) model path \(model.wavFilePath ?? "")")
         }
+        BFLog(message: "时间范围: currentAssetProgress\(currentAssetProgress.seconds)")
+        
+        let spitTime = (recordItem?.voiceChangeStickers.count ?? 0) > 0 ? 0.0 : 0.333
        
         if let mod = useVoiceStickers.first(where: { m in
-            CMTimeCompare(m.startCMTime, currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000)) <= 0 && CMTimeCompare(m.endCMTime, (currentAssetProgress + CMTime(seconds: 0.033, preferredTimescale: 1000))) > 0
+            CMTimeCompare(m.startCMTime, currentAssetProgress + CMTime(seconds: spitTime, preferredTimescale: 1000)) <= 0 && CMTimeCompare(m.endCMTime, (currentAssetProgress + CMTime(seconds: spitTime, preferredTimescale: 1000))) > 0
         }){
             for model in useVoiceStickers {
-                BFLog(message: " 时间范围:\(model.startCMTime.seconds) 到 \(model.endCMTime.seconds) \(currentAssetProgress.seconds)")
+                BFLog(message: "时间范围:\(model.startCMTime.seconds) 到 \(model.endCMTime.seconds) \(currentAssetProgress.seconds)")
             }
             recordPlayer?.replaceCurrentItem(with: AVPlayerItem(url: URL(fileURLWithPath: mod.wavFilePath)))
             let tt = currentAssetProgress - mod.startCMTime
@@ -496,7 +504,13 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
                 guard let wself = self else {
                     return
                 }
-                wself.playRecordVoice()
+//                //录制的视频如果不是播放过程当中 就不进行录音的播放了。
+                BFLog(message: "recordPlayer 播放完一段了 wself.avplayer.timeControlStatus \(wself.avplayer.timeControlStatus.rawValue)")
+                if(wself.avplayer.timeControlStatus == .playing ){
+                   
+                    wself.findAvailableChangeVoice()
+              
+                }
       
             }
              
@@ -504,43 +518,56 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
         else{
             
             BFLog(message: "没有命中查找最近一个可用的")
-            dispatchQueue = DispatchQueue.global(qos: .background) // create queue
-            dispatchWorkItem = DispatchWorkItem {[weak self] in
-                guard let wself = self else {
-                    return
-                }
-                wself.playRecordVoice(needPlay: true)
-            }
-            
-            let afterTime = findAvailableChangeVoice().seconds
-            BFLog(message: "\(afterTime)s 后自动播放变音音频")
-            if self.dispatchWorkItem != nil {
-                 dispatchQueue?.asyncAfter(
-                    deadline: .now() + afterTime,
-                     execute: self.dispatchWorkItem!
-                 )
-            }
+
+ 
+            findAvailableChangeVoice()
+        
              
        
         }
     }
     
     //add by ak 正常播放未命中可使用录音时查找最近一个可用的音频, 返回和当前播放进度的时间差
-    func findAvailableChangeVoice() -> CMTime{
+    func findAvailableChangeVoice(){
+        var playCMTime =  CMTime.zero
+        
         if let mod = recordItem?.getUsedVoices().first(where: { m in
             CMTimeCompare(m.startCMTime, currentAssetProgress) >= 0
         }){
             BFLog(message: "当前时间\(currentAssetProgress.seconds),命中的开始时间:\(mod.startCMTime.seconds) 结束时间\(mod.endCMTime.seconds)")
-            return CMTimeSubtract(mod.startCMTime, currentAssetProgress)
+            playCMTime = CMTimeSubtract(mod.startCMTime, currentAssetProgress)
+        }
+  
+        dispatchQueue = DispatchQueue.global(qos: .background) // create queue
+        dispatchWorkItem = DispatchWorkItem {[weak self] in
+            guard let wself = self else {
+                return
+            }
+                if(!wself.dispatchWorkItemIsCancel){
+                wself.playRecordVoice(needPlay: true)
+                }else{
+                    BFLog(message: "dispatchWorkItem  Is Cancel")
+                }
+           
+        }
+        
+        if self.dispatchWorkItem != nil {
+            BFLog(message: "\(playCMTime.seconds) s 后自动播放变音音频")
+             dispatchQueue?.asyncAfter(
+                deadline: .now() + playCMTime.seconds,
+                 execute: self.dispatchWorkItem!
+             )
         }
-        return CMTime.zero
+        
     }
     
     // add by ak取消自动播放任务,1,暂停时 2,录制的视频播放结束时 调用
     func cancelDispatchWorkItem(){
+        BFLog(message: "取消录制音频的查找下一个播放任务");
         //取消变声音音频任务
         if let dispatchWorkItem = self.dispatchWorkItem {
             dispatchWorkItem.cancel()
+            dispatchWorkItemIsCancel = true
         }
     }
     
@@ -579,7 +606,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
                     }
                 })
             }
-            
+            dispatchWorkItemIsCancel = false
             playRecordVoice(needPlay: needPlay)
         }else {
             locationTo(time: currentAssetProgress)
@@ -638,6 +665,8 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
     }
     
     override func pause() {
+        
+        dispatchWorkItemIsCancel = true
         avplayer.pause()
         recordPlayer?.pause()
         
@@ -648,8 +677,7 @@ class BFRecordScreenCameraManager : BFRecordScreenBaseManager{
             camera?.resumeCameraCapture()
             playerCoverIV.isHidden = true
         }
-      
-        cancelDispatchWorkItem()
+     
     }
     
     override func dragEnd() {