فهرست منبع

Merge branch 'master' of https://git.yishihui.com/iOS/BFRecordScreenKit

* 'master' of https://git.yishihui.com/iOS/BFRecordScreenKit:
  1.修改字幕识别回调
harry 3 سال پیش
والد
کامیت
beaf5cea3e
1فایلهای تغییر یافته به همراه31 افزوده شده و 30 حذف شده
  1. 31 30
      BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

+ 31 - 30
BFRecordScreenKit/Classes/RecordScreen/Controller/BFRecordScreenController.swift

@@ -494,7 +494,6 @@ public class BFRecordScreenController: BFBaseViewController {
 
             let header = dicResult?["header"] as? [String: Any]
             let payload = dicResult?["payload"] as? [String: Any]
-
             BFLog(1, message: "识别结果:) \(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)")
 
             DispatchQueue.main.async {
@@ -502,44 +501,46 @@ public class BFRecordScreenController: BFBaseViewController {
 
                 let newSubtitle = PQEditSubTitleModel()
                 // 任务全局唯一ID,请记录该值,便于排查问题。 每次 startRecorder 和 stopRecoder 之间  task_Id都不会变化
-                newSubtitle.taskID = (header?["task_id"] as? String) ?? ""
+                let taskID = header?["task_id"] as? String
+                newSubtitle.taskID = taskID ?? ""
                 BFLog(1, message: "对应关系:字幕所属地址:\((audioFilePath ?? "b").replacingOccurrences(of: documensDirectory, with: "")), 开始录音输入:\((self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").replacingOccurrences(of: documensDirectory, with: ""))")
                 // 这里加300ms 是因为返回结果为了切到字,时长提前一些时间,具体时间官方没说和原音频有关系。这里我们先延后300ms 单位:毫秒。
-                if let audioUrl = audioFilePath, URL(fileURLWithPath: audioUrl).deletingPathExtension().lastPathComponent.contains(URL(fileURLWithPath: self?.recorderManager?.voiceModel?.wavFilePath ?? "aa").deletingPathExtension().lastPathComponent) {
-                    newSubtitle.timelineIn = (self?.recorderManager?.voiceModel?.startCMTime ?? .zero) + CMTime(seconds: Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0, preferredTimescale: 1000)
-                    newSubtitle.timelineOut = (self?.recorderManager?.voiceModel?.startCMTime ?? .zero) + CMTime(seconds: Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0, preferredTimescale: 1000)
-                    newSubtitle.audioFilePath = audioUrl
-                    BFLog(1, message: "字幕按时回来")
-
-                    if (newSubtitle.timelineIn - (self?.recorderManager?.voiceModel?.endCMTime ?? .zero)).seconds > 0.1 {
-                        BFLog(1, message: "卡在录音尾巴上了1")
-                        newSubtitle.timelineIn = newSubtitle.timelineIn - CMTime(seconds: 0.1, preferredTimescale: 1000)
-                    }
-                } else {
-                    if let voice = self?.itemModels[self?.currItemModelIndex ?? 0].voiceStickers.first(where: { m in
-                        m.wavFilePath == audioFilePath
-                    }) {
-                        newSubtitle.timelineIn = voice.startCMTime + CMTime(seconds: Float64((((payload?["begin_time"]) as? Int) ?? 0) + 300) / 1000.0, preferredTimescale: 1000)
-                        newSubtitle.timelineOut = voice.startCMTime + CMTime(seconds: Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0, preferredTimescale: 1000)
-                        newSubtitle.audioFilePath = voice.wavFilePath
-                        BFLog(1, message: "字幕没及时回来")
-                        if (newSubtitle.timelineIn - voice.endCMTime).seconds > 0.1 {
-                            BFLog(1, message: "卡在录音尾巴上了1")
-                            newSubtitle.timelineIn = newSubtitle.timelineIn - CMTime(seconds: 0.1, preferredTimescale: 1000)
+                var tempVoice:PQVoiceModel?
+                var tempItem:BFRecordItemModel?
+                self?.itemModels.forEach({ item in
+                    if tempVoice == nil {
+                        tempVoice = item.voiceStickers.first { voice in
+                            voice.titleTaskId == taskID
+                        }
+                        if tempVoice != nil {
+                            tempItem = item
                         }
-                    } else {
-                        BFLog(1, message: "没有对应音频播放记录,出现错误!!!!!!")
-                        return
                     }
+                })
+                guard let currentVoice = tempVoice ?? self?.recorderManager?.voiceModel else {
+                    return
+                }
+                guard let currentItem = tempItem ?? self?.itemModels[self?.currItemModelIndex ?? 0] else {
+                    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.timelineOut = currentVoice.startCMTime + CMTime(seconds: Float64(((payload?["time"]) as? Int) ?? 0) / 1000.0, preferredTimescale: 1000)
+                newSubtitle.audioFilePath = currentVoice.wavFilePath
+                BFLog(1, message: "字幕按时回来")
+                BFLog(3, message: "字幕返回begin:\(((payload?["begin_time"]) as? Int) ?? 0),timelineIn:\(newSubtitle.timelineIn.seconds), end:\(((payload?["time"]) as? Int) ?? 0),timelineOut:\(newSubtitle.timelineOut.seconds)")
+                if (newSubtitle.timelineIn - currentVoice.startCMTime).seconds > 0.1 {
+                    BFLog(1, message: "卡在录音尾巴上了1")
+                    newSubtitle.timelineIn = newSubtitle.timelineIn - CMTime(seconds: 0.1, preferredTimescale: 1000)
                 }
                 let showText = ((payload?["result"]) as? String) ?? ""
                 newSubtitle.text = showText
 //                newSubtitle.audioFilePath = audioFilePath ?? ""
-                BFLog(1, message: "添加字幕数据 timelineIn \(newSubtitle.timelineIn.seconds) timelineOut \(newSubtitle.timelineOut.seconds) text: \(newSubtitle.text) 音频路径为:\(audioFilePath ?? "bb") 传入的地址:\(self?.recorderManager?.voiceModel?.wavFilePath ?? "aa")")
-
+                BFLog(1, message: "添加字幕数据 timelineIn \(newSubtitle.timelineIn.seconds) timelineOut \(newSubtitle.timelineOut.seconds) text: \(newSubtitle.text) 音频路径为:\(audioFilePath ?? "bb") 传入的地址:\(currentVoice.wavFilePath ?? "aa")")
                 newSubtitle.setting = self?.subtitleSettingView.subtitle.setting ?? BFSubTitileSettingModel()
-
-                self?.itemModels[self?.currItemModelIndex ?? 0].titleStickers.append(newSubtitle)
+                currentItem.titleStickers.append(newSubtitle)
             }
         }