Sfoglia il codice sorgente

1.优化字幕返回

wenweiwei 3 anni fa
parent
commit
294d6758ab

+ 3 - 3
BFRecordScreenKit/Classes/BFVoiceRecordManager.swift

@@ -20,7 +20,7 @@ class BFVoiceRecordManager: NSObject {
     // 录音进度回调
     // 录音进度回调
     var recorderProgrossHandle: ((Float64) -> Void)?
     var recorderProgrossHandle: ((Float64) -> Void)?
     // 字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
     // 字幕的回调 参数1: 字幕数据 ,参数2 :对应的录音文件
-    var subtitleRecordHandle: ((String?, String?, Bool) -> Void)?
+    var subtitleRecordHandle: ((_ eventCode: Int32,_ recordId:String?, String?, String?, Bool) -> Void)?
 
 
     // 字幕服务 dubug信息
     // 字幕服务 dubug信息
     var NeoNuiDebugHandle: ((String?) -> Void)?
     var NeoNuiDebugHandle: ((String?) -> Void)?
@@ -144,10 +144,10 @@ extension BFVoiceRecordManager: BFRecorderManagerDelegate {
     }
     }
 
 
     // 字幕返回
     // 字幕返回
-    public func eventCallback(_: BFRecorderManager, asrResult: String, audioFilePath: String, isFinish: Bool) {
+    public func eventCallback(_ manager: BFRecorderManager, eventCode: Int32, asrResult: String, audioFilePath: String, isFinish: Bool) {
         // 最后输出的文件是降噪后的
         // 最后输出的文件是降噪后的
         let noiseFilePath = audioFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")
         let noiseFilePath = audioFilePath.replacingOccurrences(of: ".pcm", with: "_noise.wav")
-        subtitleRecordHandle?(asrResult, noiseFilePath, isFinish)
+        subtitleRecordHandle?(eventCode,voiceModel?.recordId, asrResult, noiseFilePath, isFinish)
     }
     }
 
 
     public func neoNuiDebugHandle(_ msg: String) {
     public func neoNuiDebugHandle(_ msg: String) {

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

@@ -34,6 +34,7 @@ public class BFRecordScreenController: BFBaseViewController {
     public var recordEndHandle: ((_ currentRecord: PQVoiceModel?) -> Void)?
     public var recordEndHandle: ((_ currentRecord: PQVoiceModel?) -> Void)?
     // 字幕按钮点击上报
     // 字幕按钮点击上报
     public var subTitleBtnClickHandle: ((_ isOn: Bool) -> Void)?
     public var subTitleBtnClickHandle: ((_ isOn: Bool) -> Void)?
+    public var currentRecordId: String? // 当前录制Id
 
 
     // MARK: - 录制参数
     // MARK: - 录制参数
 
 
@@ -484,11 +485,21 @@ public class BFRecordScreenController: BFBaseViewController {
         }
         }
 
 
         // 录音字幕回调
         // 录音字幕回调
-        recorderManager?.subtitleRecordHandle = { [weak self] asrResult, audioFilePath, _ in
-            if asrResult == nil {
+        recorderManager?.subtitleRecordHandle = { [weak self] eventCode, recordId, asrResult, audioFilePath, _ in
+            if asrResult == nil || (asrResult?.count ?? 0) <= 0 {
                 BFLog(message: "识别结果为空????不能生成字幕数据")
                 BFLog(message: "识别结果为空????不能生成字幕数据")
                 return
                 return
             }
             }
+            if eventCode == 2 || eventCode == 26 {
+                self?.currentRecordId = nil
+            }
+            if eventCode == 0 {
+                self?.currentRecordId = recordId
+            }
+            // 只有为24时才有字幕返回
+            if eventCode != 24 {
+                return
+            }
             DispatchQueue.global().async {
             DispatchQueue.global().async {
                 let dicResult: [String: Any]? = jsonStringToDictionary(asrResult!)
                 let dicResult: [String: Any]? = jsonStringToDictionary(asrResult!)
                 let header = dicResult?["header"] as? [String: Any]
                 let header = dicResult?["header"] as? [String: Any]
@@ -499,9 +510,8 @@ public class BFRecordScreenController: BFBaseViewController {
                 let newSubtitle = PQEditSubTitleModel()
                 let newSubtitle = PQEditSubTitleModel()
                 // 任务全局唯一ID,请记录该值,便于排查问题。 每次 startRecorder 和 stopRecoder 之间  task_Id都不会变化
                 // 任务全局唯一ID,请记录该值,便于排查问题。 每次 startRecorder 和 stopRecoder 之间  task_Id都不会变化
                 let taskID = header?["task_id"] as? String
                 let taskID = header?["task_id"] as? String
-                let finish = header?["task_id"] as? String
-
                 newSubtitle.taskID = taskID ?? ""
                 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: ""))")
                 BFLog(1, message: "对应关系:字幕所属地址:taskID:\(taskID ?? ""),\((audioFilePath ?? "b").replacingOccurrences(of: documensDirectory, with: "")), 开始录音输入:\((self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").replacingOccurrences(of: documensDirectory, with: ""))")
                 // 这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
                 // 这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
                 var tempVoice: PQVoiceModel?
                 var tempVoice: PQVoiceModel?
@@ -510,7 +520,7 @@ public class BFRecordScreenController: BFBaseViewController {
                 self?.itemModels.enumerated().forEach { index, item in
                 self?.itemModels.enumerated().forEach { index, item in
                     if tempVoice == nil {
                     if tempVoice == nil {
                         tempVoice = item.voiceStickers.first { voice in
                         tempVoice = item.voiceStickers.first { voice in
-                            voice.titleTaskId == taskID
+                            voice.recordId == self?.currentRecordId
                         }
                         }
                         if tempVoice != nil {
                         if tempVoice != nil {
                             tempItem = item
                             tempItem = item
@@ -519,7 +529,7 @@ public class BFRecordScreenController: BFBaseViewController {
                     }
                     }
                 }
                 }
                 // 2:如果通过titleTaskId没找到录音文件,则通过
                 // 2:如果通过titleTaskId没找到录音文件,则通过
-                if tempVoice == nil && (self?.isRecording ?? false) && self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.last?.titleTaskId == nil {
+                if tempVoice == nil && (self?.isRecording ?? false) && self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.last?.recordId == nil {
                     BFLog(3, message: "字幕回调-如果通过titleTaskId没找到录音文件:taskID=\(taskID ?? ""),audioFilePath=\(audioFilePath ?? "")")
                     BFLog(3, message: "字幕回调-如果通过titleTaskId没找到录音文件:taskID=\(taskID ?? ""),audioFilePath=\(audioFilePath ?? "")")
                     tempVoice = self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.last
                     tempVoice = self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.last
                 }
                 }
@@ -532,9 +542,6 @@ public class BFRecordScreenController: BFBaseViewController {
                     BFLog(3, message: "字幕回调-最终没找到录音文件:taskID=\(taskID ?? ""),audioFilePath=\(audioFilePath ?? "")")
                     BFLog(3, message: "字幕回调-最终没找到录音文件:taskID=\(taskID ?? ""),audioFilePath=\(audioFilePath ?? "")")
                     return
                     return
                 }
                 }
-                if currentVoice.titleTaskId == nil {
-                    currentVoice.titleTaskId = taskID
-                }
                 newSubtitle.timelineIn = currentVoice.startCMTime + CMTime(seconds: Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0, preferredTimescale: 1000)
                 newSubtitle.timelineIn = currentVoice.startCMTime + CMTime(seconds: Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0, preferredTimescale: 1000)
                 newSubtitle.timelineOut = currentVoice.startCMTime + CMTime(seconds: Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0, preferredTimescale: 1000)
                 newSubtitle.timelineOut = currentVoice.startCMTime + CMTime(seconds: Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0, preferredTimescale: 1000)
                 newSubtitle.audioFilePath = currentVoice.wavFilePath
                 newSubtitle.audioFilePath = currentVoice.wavFilePath
@@ -731,11 +738,10 @@ public class BFRecordScreenController: BFBaseViewController {
         if !SWNetRequest.isNetReachabled() {
         if !SWNetRequest.isNetReachabled() {
             cShowHUB(superView: view, msg: "网络不佳,字幕可能无法生成")
             cShowHUB(superView: view, msg: "网络不佳,字幕可能无法生成")
         }
         }
-        if(PQENVUtil.shared.channel == "Development"){
+        if PQENVUtil.shared.channel == "Development" {
             view.addSubview(neoNuiDebugLabel)
             view.addSubview(neoNuiDebugLabel)
             view.addSubview(audioQueueRecoderLabel)
             view.addSubview(audioQueueRecoderLabel)
         }
         }
- 
     }
     }
 
 
     @objc func editSubtitle() {
     @objc func editSubtitle() {
@@ -982,7 +988,7 @@ public class BFRecordScreenController: BFBaseViewController {
 
 
         // 从原数组中删除
         // 从原数组中删除
         itemModels[currItemModelIndex].titleStickers.removeAll { m in
         itemModels[currItemModelIndex].titleStickers.removeAll { m in
-            let su = (m.taskID == voiceModel.titleTaskId)
+            let su = (m.recordId == voiceModel.recordId)
             if su {
             if su {
                 deleteTemp.append(m)
                 deleteTemp.append(m)
             }
             }
@@ -1021,7 +1027,7 @@ public class BFRecordScreenController: BFBaseViewController {
                         item.endCMTime = item.endCMTime - currDuration
                         item.endCMTime = item.endCMTime - currDuration
 
 
                         let titlsList = itemModels[currItemModelIndex].titleStickers.filter { tm in
                         let titlsList = itemModels[currItemModelIndex].titleStickers.filter { tm in
-                            tm.taskID == item.titleTaskId
+                            tm.recordId == item.recordId
                         }
                         }
 
 
                         for titleM in titlsList {
                         for titleM in titlsList {
@@ -1077,6 +1083,7 @@ public class BFRecordScreenController: BFBaseViewController {
         let model = PQVoiceModel()
         let model = PQVoiceModel()
         model.startCMTime = currentAssetProgress
         model.startCMTime = currentAssetProgress
         model.currIndex = currItemModelIndex
         model.currIndex = currItemModelIndex
+        model.recordId = getUniqueId(desc: "recordId")
         model.volume = 100
         model.volume = 100
         recorderManager?.voiceModel = model
         recorderManager?.voiceModel = model
         BFLog(3, message: "开始录制-开始:currentAssetProgress=\(currentAssetProgress.seconds),cuInde=\(currItemModelIndex),\(model)")
         BFLog(3, message: "开始录制-开始:currentAssetProgress=\(currentAssetProgress.seconds),cuInde=\(currItemModelIndex),\(model)")
@@ -1169,7 +1176,7 @@ public class BFRecordScreenController: BFBaseViewController {
         BFLog(2, message: "删除\(voiceModel?.wavFilePath ?? "")对应的字幕 前 count\(subtitleCount)")
         BFLog(2, message: "删除\(voiceModel?.wavFilePath ?? "")对应的字幕 前 count\(subtitleCount)")
         if subtitleCount > 0 {
         if subtitleCount > 0 {
             for title in itemModels[currItemModelIndex].titleStickers {
             for title in itemModels[currItemModelIndex].titleStickers {
-                if title.taskID == voiceModel?.titleTaskId ?? "none" {
+                if title.recordId == voiceModel?.recordId ?? "none" {
                     if let index = itemModels[currItemModelIndex].titleStickers.firstIndex(of: title) {
                     if let index = itemModels[currItemModelIndex].titleStickers.firstIndex(of: title) {
                         itemModels[currItemModelIndex].titleStickers.remove(at: index)
                         itemModels[currItemModelIndex].titleStickers.remove(at: index)
                     }
                     }
@@ -1253,11 +1260,11 @@ public class BFRecordScreenController: BFBaseViewController {
                                 // 注:之后的录音和字幕增加对应的时长
                                 // 注:之后的录音和字幕增加对应的时长
                                 item.startCMTime = item.startCMTime + currDuration
                                 item.startCMTime = item.startCMTime + currDuration
                                 item.endCMTime = item.endCMTime + currDuration
                                 item.endCMTime = item.endCMTime + currDuration
-                                
-                                let titlsList = itemModels[currItemModelIndex].titleStickers.filter({ tm in
-                                    tm.taskID == item.titleTaskId
-                                })
-                                
+
+                                let titlsList = itemModels[currItemModelIndex].titleStickers.filter { tm in
+                                    tm.recordId == item.recordId
+                                }
+
                                 for titleM in titlsList {
                                 for titleM in titlsList {
                                     titleM.timelineIn = titleM.timelineIn + currDuration
                                     titleM.timelineIn = titleM.timelineIn + currDuration
                                     titleM.timelineOut = titleM.timelineOut + currDuration
                                     titleM.timelineOut = titleM.timelineOut + currDuration