Browse Source

字幕逻辑修改

harry 3 years ago
parent
commit
63113c2d91

+ 14 - 1
BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

@@ -21,6 +21,9 @@ class BFVoiceRecordManager: NSObject {
     var recorderProgrossHandle: ((Float64) -> Void)?
     // 字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
     var subtitleRecordHandle: ((_ eventCode: Int32,_ recordId:String?, String?, String?, Bool) -> Void)?
+    
+    // 当前录制Id
+    public var currentRecordId: String?
 
     // 字幕服务 dubug信息
     var NeoNuiDebugHandle: ((String?,Bool) -> Void)?
@@ -147,7 +150,17 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
     public func eventCallback(_ manager: BFRecorderManager, eventCode: Int32, asrResult: String, audioFilePath: String, isFinish: Bool) {
         // 最后输出的文件是降噪后的
         let noiseFilePath = audioFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")
-        subtitleRecordHandle?(eventCode,voiceModel?.recordId, asrResult, noiseFilePath, isFinish)
+        let tmprecordId = currentRecordId
+        if eventCode == 26 {
+            currentRecordId = nil
+        }
+        if eventCode == 0 {
+            currentRecordId = getUniqueId(desc: "currentRecordId")
+            if voiceModel?.recordId?.count == nil {
+                voiceModel?.recordId = currentRecordId
+            }
+        }
+        subtitleRecordHandle?(eventCode, tmprecordId, asrResult, noiseFilePath, isFinish)
     }
 
 

+ 26 - 26
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -34,7 +34,7 @@ public class BFRecordScreenController: BFBaseViewController {
     public var recordEndHandle: ((_ currentRecord: PQVoiceModel?) -> Void)?
     // 字幕按钮点击上报
     public var subTitleBtnClickHandle: ((_ isOn: Bool) -> Void)?
-    public var currentRecordId: String? // 当前录制Id
+//    public var currentRecordId: String? // 当前录制Id
 
     // MARK: - 录制参数
 
@@ -486,34 +486,34 @@ public class BFRecordScreenController: BFBaseViewController {
 
         // 录音字幕回调
         recorderManager?.subtitleRecordHandle = { [weak self] eventCode, recordId, asrResult, audioFilePath, _ in
-            if asrResult == nil || (asrResult?.count ?? 0) <= 0 {
-                BFLog(message: "识别结果为空????不能生成字幕数据")
-                return
-            }
-            if eventCode == 26 {
-                self?.currentRecordId = nil
-            }
-            if eventCode == 0 {
-                self?.currentRecordId = recordId
-            }
-            // 只有为24时才有字幕返回
-            if eventCode != 24 || recordId == nil{
-                return
-            }
+            BFLog(1, message: "eventcode:\(eventCode), recordid:\(recordId), asr:\(asrResult)")
+            
             DispatchQueue.global().async { [weak self] in
+                let newSubtitle = PQEditSubTitleModel()
+                newSubtitle.recordId = recordId
+//                    if eventCode == 26 {
+//                        self?.currentRecordId = nil
+//                    }
+//                    if eventCode == 0 {
+//                        self?.currentRecordId = recordId
+//                    }
+                if asrResult == nil || (asrResult?.count ?? 0) <= 0 {
+                    BFLog(1, message: "onNuiEventCallback 识别结果为空????不能生成字幕数据")
+                    return
+                }
+                // 只有为24时才有字幕返回
+                if eventCode != 24 || recordId == nil{
+                    BFLog(1, message: "onNuiEventCallback eventCode != 24 || recordId == nil")
+
+                    return
+                }
+                
                 let dicResult: [String: Any]? = jsonStringToDictionary(asrResult!)
                 let header = dicResult?["header"] as? [String: Any]
                 let payload = dicResult?["payload"] as? [String: Any]
                 BFLog(1, message: "onNuiEventCallback event 识别结果:) \(payload?["result"] ?? "") ,taskId:\((header?["task_id"] as? String) ?? "taskId"), 识别时间:\(((payload?["begin_time"]) as? Int) ?? 0) ~ \(((payload?["time"]) as? Int) ?? 0) startTime:\(self?.recorderManager?.voiceModel?.startCMTime.seconds ?? 0.0)")
                 // 1,保存字幕数据 begin_time是开始出现文字的时间,time 是结束文字出现的时间 单位都为毫秒,都是相对于录制音频数据整段时间。self.recorderManager.voiceModel?.startCMTime.seconds 为开始的录制的时间,开始和结束都要加上这个时差
-
-                let newSubtitle = PQEditSubTitleModel()
-                // 任务全局唯一ID,请记录该值,便于排查问题。 每次 startRecorder 和 stopRecoder 之间  task_Id都不会变化
-                let taskID = header?["task_id"] as? String
-
-                newSubtitle.taskID = taskID ?? ""
-                newSubtitle.recordId = self?.currentRecordId
-                BFLog(1, message: "对应关系:字幕所属地址:taskID:\(taskID ?? ""),\((audioFilePath ?? "b").replacingOccurrences(of: documensDirectory, with: "")), 开始录音输入:\((self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").replacingOccurrences(of: documensDirectory, with: ""))")
+                let taskID = "aa"
                 // 这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
                 var tempVoice: PQVoiceModel?
                 var tempItem: BFRecordItemModel?
@@ -794,7 +794,7 @@ public class BFRecordScreenController: BFBaseViewController {
         for (index, subtitle) in itemModels[currItemModelIndex].titleStickers.enumerated() {
             if CMTimeCompare(subtitle.timelineIn, time) <= 0, CMTimeCompare(subtitle.timelineOut, time) > 0, subtitle.audioFilePath.count > 0 { //  audioFilePath.count 这个条件是确保这个字幕有对应录音
                 findShowSubtitle = subtitle
-                BFLog(1, message: "找到要显示的字幕 in \((findShowSubtitle?.timelineIn ?? .zero).seconds) out \((findShowSubtitle?.timelineOut ?? .zero).seconds) text:\(findShowSubtitle?.text ?? "") currTime is \(CMTimeGetSeconds(time))")
+//                BFLog(1, message: "找到要显示的字幕 in \((findShowSubtitle?.timelineIn ?? .zero).seconds) out \((findShowSubtitle?.timelineOut ?? .zero).seconds) text:\(findShowSubtitle?.text ?? "") currTime is \(CMTimeGetSeconds(time))")
                 showSubtitleIndex = index
                 break
             }
@@ -1091,7 +1091,7 @@ public class BFRecordScreenController: BFBaseViewController {
         let model = PQVoiceModel()
         model.startCMTime = currentAssetProgress
         model.currIndex = currItemModelIndex
-        model.recordId = getUniqueId(desc: "recordId")
+//        model.recordId = getUniqueId(desc: "recordId")
         model.volume = 100
         recorderManager?.voiceModel = model
         BFLog(3, message: "开始录制-开始:currentAssetProgress=\(currentAssetProgress.seconds),cuInde=\(currItemModelIndex),\(model)")
@@ -1534,7 +1534,7 @@ public class BFRecordScreenController: BFBaseViewController {
             BFLog(3, message: "未找到可播放录音")
             return
         }
-        BFLog(1, message: "当前时间:\(CMTimeGetSeconds(currentT)), 找到的音频:\(recordedAudio.startCMTime.seconds) ~ \(recordedAudio.endCMTime.seconds), \(recordedAudio.wavFilePath ?? "")")
+//        BFLog(1, message: "当前时间:\(CMTimeGetSeconds(currentT)), 找到的音频:\(recordedAudio.startCMTime.seconds) ~ \(recordedAudio.endCMTime.seconds), \(recordedAudio.wavFilePath ?? "")")
 
         // 创建播放器
         if recordPlayer == nil || (recordPlayer?.currentItem?.asset as? AVURLAsset)?.url.lastPathComponent != (recordedAudio.wavFilePath as NSString?)?.lastPathComponent {