Pārlūkot izejas kodu

字幕统一数据类型

jsonwang 3 gadi atpakaļ
vecāks
revīzija
fa43e9535d

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

@@ -69,6 +69,9 @@ public class BFRecordScreenController: BFBaseViewController {
     var events = [WithDrawModel]()              // 行为记录,方便撤销
     var events = [WithDrawModel]()              // 行为记录,方便撤销
     var isDragingProgressSlder : Bool = false   // 是否在拖动进度条
     var isDragingProgressSlder : Bool = false   // 是否在拖动进度条
     var isStopAtRecordRange = -1
     var isStopAtRecordRange = -1
+    
+    //保存识别出来的字幕信息,用于回放,和合成使用
+    var saveSubTitles:[PQEditSubTitleModel] = Array.init()
     //定义音频的编码参数
     //定义音频的编码参数
     let recordSettings:[String : Any] = [AVSampleRateKey : 44100.0, // 声音采样率
     let recordSettings:[String : Any] = [AVSampleRateKey : 44100.0, // 声音采样率
                                          AVFormatIDKey : kAudioFormatLinearPCM,  // 编码格式
                                          AVFormatIDKey : kAudioFormatLinearPCM,  // 编码格式
@@ -266,11 +269,11 @@ public class BFRecordScreenController: BFBaseViewController {
         
         
         let test = ""
         let test = ""
         let leftPoint:CGFloat = 37
         let leftPoint:CGFloat = 37
-        let height = sizeWithText(text: test, font: UIFont.systemFont(ofSize: 70 * 375 / 1080), size: CGSize(width:  cScreenWidth - leftPoint * 2, height: 102))
+        let height = sizeWithText(text: test, font: UIFont.boldSystemFont(ofSize: 70 * 375 / 1080), size: CGSize(width:  cScreenWidth - leftPoint * 2, height: 102))
        
        
         let subtitleLabel = PQSubTitleLabel.init(frame: CGRect.init(x: leftPoint, y:  (cScreenHeigth * 0.70 - height.height) , width: cScreenWidth - 37 * 2, height: height.height))
         let subtitleLabel = PQSubTitleLabel.init(frame: CGRect.init(x: leftPoint, y:  (cScreenHeigth * 0.70 - height.height) , width: cScreenWidth - 37 * 2, height: height.height))
         subtitleLabel.textAlignment = .center
         subtitleLabel.textAlignment = .center
-        subtitleLabel.text = "test"
+        subtitleLabel.text = test
  
  
         subtitleLabel.numberOfLines = 0
         subtitleLabel.numberOfLines = 0
         subtitleLabel.lineBreakMode = .byClipping
         subtitleLabel.lineBreakMode = .byClipping
@@ -379,10 +382,10 @@ public class BFRecordScreenController: BFBaseViewController {
         
         
         //字幕设置回调
         //字幕设置回调
         //设置默认值
         //设置默认值
-        setSubtitleStyle(settingModel: subtitleSettingView.subtitleSetting)
-        subtitleSettingView.subtitleSettingCallBack = {[weak self] settingModel in
+        setSubtitleStyle(settingModel: subtitleSettingView.subtitle.setting)
+        subtitleSettingView.subtitleSettingCallBack = {[weak self] subtitileModel in
             
             
-            self?.setSubtitleStyle(settingModel: settingModel)
+            self?.setSubtitleStyle(settingModel: subtitileModel.setting)
             
             
         }
         }
         subtitleEditView.editSubtitleDone = {[weak self] newtext in
         subtitleEditView.editSubtitleDone = {[weak self] newtext in
@@ -398,26 +401,33 @@ public class BFRecordScreenController: BFBaseViewController {
         BFLog(message: "编辑字幕")
         BFLog(message: "编辑字幕")
         subtitleEditView.isHidden = false
         subtitleEditView.isHidden = false
         subtitleEditView.textView.becomeFirstResponder()
         subtitleEditView.textView.becomeFirstResponder()
-        subtitleEditView.settingModel = subtitleSettingView.subtitleSetting
-        
         subtitleEditView.setNewText(text: subtitleLabel.text ?? "")
         subtitleEditView.setNewText(text: subtitleLabel.text ?? "")
     }
     }
-    func setSubtitleStyle(settingModel:BFSubtitileSettingModel) {
+    
+    /// 更新字幕,在回放时使用
+    /// - Parameter time: 当前播放的进度
+    func updateSubtitle(time:CMTime) {
+         
+    }
+    
+    /// 设置字幕样式和位置
+    /// - Parameter settingModel: 样式model
+    func setSubtitleStyle(settingModel:BFSubTitileSettingModel) {
         
         
         if(settingModel.subtitleIsShow){
         if(settingModel.subtitleIsShow){
             //设置样式
             //设置样式
             subtitleLabel.strokeColor = settingModel.strokeColor
             subtitleLabel.strokeColor = settingModel.strokeColor
             subtitleLabel.strokeWidth = settingModel.strokeWidth
             subtitleLabel.strokeWidth = settingModel.strokeWidth
-            subtitleLabel.font = UIFont.systemFont(ofSize: CGFloat(settingModel.subtitleSize) * 375 / 1080)
+            subtitleLabel.font = UIFont.boldSystemFont(ofSize: CGFloat(settingModel.subtitleSize) * 375 / 1080)
             subtitleLabel.textColor = settingModel.fontColor
             subtitleLabel.textColor = settingModel.fontColor
  
  
             let leftPoint:CGFloat = 37
             let leftPoint:CGFloat = 37
-            let height = sizeWithText(text: self.subtitleLabel.text ?? "", font: UIFont.systemFont(ofSize: 70 * 375 / 1080), size: CGSize(width:  cScreenWidth - leftPoint * 2, height: 102))
+            let height = sizeWithText(text: self.subtitleLabel.text ?? "", font: UIFont.systemFont(ofSize: CGFloat(settingModel.subtitleSize * 375 / 1080)), size: CGSize(width:  cScreenWidth - leftPoint * 2, height: 102))
         
         
             //下 //设置位置
             //下 //设置位置
-            if(self.subtitleSettingView.subtitleSetting.subtitlePoint == 0){
+            if(self.subtitleSettingView.subtitle.setting.subtitlePoint == 0){
                 self.subtitleLabel.frame =  CGRect.init(x: leftPoint, y:  (cScreenHeigth * 0.70 - height.height) , width: cScreenWidth - 37 * 2, height: height.height)
                 self.subtitleLabel.frame =  CGRect.init(x: leftPoint, y:  (cScreenHeigth * 0.70 - height.height) , width: cScreenWidth - 37 * 2, height: height.height)
-            }else if(self.subtitleSettingView.subtitleSetting.subtitlePoint == 1){//中
+            }else if(self.subtitleSettingView.subtitle.setting.subtitlePoint == 1){//中
                 self.subtitleLabel.frame =  CGRect.init(x: leftPoint, y:  (cScreenHeigth / 2.0 - height.height / 2.0) , width: cScreenWidth - 37 * 2, height: height.height)
                 self.subtitleLabel.frame =  CGRect.init(x: leftPoint, y:  (cScreenHeigth / 2.0 - height.height / 2.0) , width: cScreenWidth - 37 * 2, height: height.height)
             }else{//上
             }else{//上
                 self.subtitleLabel.frame =  CGRect.init(x: leftPoint, y:  (cScreenHeigth * 0.12) , width: cScreenWidth - 37 * 2, height: height.height)
                 self.subtitleLabel.frame =  CGRect.init(x: leftPoint, y:  (cScreenHeigth * 0.12) , width: cScreenWidth - 37 * 2, height: height.height)
@@ -1096,17 +1106,21 @@ extension BFRecordScreenController:PQSpeechTranscriberUtilDelegate{
         
         
         let payload = dicResult?["payload"] as? Dictionary<String,Any>
         let payload = dicResult?["payload"] as? Dictionary<String,Any>
         
         
-        
         BFLog(message: "识别结果:dicResult is \(String(describing: dicResult)) \((payload?["result"])!)")
         BFLog(message: "识别结果:dicResult is \(String(describing: dicResult)) \((payload?["result"])!)")
         DispatchQueue.main.async {
         DispatchQueue.main.async {
             
             
+            //1,显示字幕
             self.subtitleLabel.text = ((payload?["result"]) as? String) ?? ""
             self.subtitleLabel.text = ((payload?["result"]) as? String) ?? ""
+            self.setSubtitleStyle(settingModel: self.subtitleSettingView.subtitle.setting)
             
             
-            self.setSubtitleStyle(settingModel: self.subtitleSettingView.subtitleSetting)
-            
+            //2,保存字幕数据
+            let newSubtitle = PQEditSubTitleModel()
+//            newSubtitle.timelineIn = subTitle.timelineIn
+//            newSubtitle.timelineOut = subTitle.timelineOut
+            newSubtitle.text = ((payload?["result"]) as? String) ?? ""
+            newSubtitle.setting = self.subtitleSettingView.subtitle.setting
+            self.saveSubTitles.append(newSubtitle)
             
             
-    
- 
         }
         }
         
         
     }
     }

+ 1 - 1
BFRecordScreenKit/Classes/RecordScreen/View/BFSubtitleEditView.swift

@@ -14,7 +14,7 @@ typealias EditSubtitleDone = (_ text: String) -> Void
 class BFSubtitleEditView: UIView {
 class BFSubtitleEditView: UIView {
     var editSubtitleDone:EditSubtitleDone?
     var editSubtitleDone:EditSubtitleDone?
     
     
-    var settingModel:BFSubtitileSettingModel = BFSubtitileSettingModel.init()
+//    var settingModel:BFSubtitileSettingModel = BFSubtitileSettingModel.init()
     /// 输入框
     /// 输入框
     lazy var textView: BFTextView = {
     lazy var textView: BFTextView = {
         let textView = BFTextView()
         let textView = BFTextView()

+ 18 - 43
BFRecordScreenKit/Classes/RecordScreen/View/BFSubtitleSettingView.swift

@@ -9,7 +9,7 @@ import Foundation
 import BFFramework
 import BFFramework
 import BFCommonKit
 import BFCommonKit
 
 
-typealias SubtitleSettingCallBack = (_ subtitileSettingModel: BFSubtitileSettingModel) -> Void
+typealias SubtitleSettingCallBack = (_ subtitileModel: PQEditSubTitleModel) -> Void
 
 
 class BFSubtitleSettingView: UIView {
 class BFSubtitleSettingView: UIView {
     
     
@@ -27,7 +27,7 @@ class BFSubtitleSettingView: UIView {
     
     
     var subtitleSettingCallBack:SubtitleSettingCallBack?
     var subtitleSettingCallBack:SubtitleSettingCallBack?
     
     
-    var subtitleSetting:BFSubtitileSettingModel = BFSubtitileSettingModel.init()
+    var subtitle:PQEditSubTitleModel = PQEditSubTitleModel.init()
     
     
     //样式配置
     //样式配置
     var styleConfig:Dictionary<Int,Dictionary<String,Any>> = Dictionary.init()
     var styleConfig:Dictionary<Int,Dictionary<String,Any>> = Dictionary.init()
@@ -225,17 +225,17 @@ class BFSubtitleSettingView: UIView {
         sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
         sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
         sender.layer.borderWidth = 1.5
         sender.layer.borderWidth = 1.5
         
         
-        subtitleSetting.subtitleStyle = sender.tag
+        subtitle.setting.subtitleStyle = sender.tag
         
         
         let config = styleConfig[sender.tag]
         let config = styleConfig[sender.tag]
         
         
-        subtitleSetting.fontColor = config?["fontColor"] as! UIColor
-        subtitleSetting.backgroundColor = config?["backgroundColor"] as! UIColor
-        subtitleSetting.backgroundAlpha = Float(config?["backgroundAlpha"] as! Double)
-        subtitleSetting.strokeColor = config?["strokeColor"] as! UIColor
+        subtitle.setting.fontColor = config?["fontColor"] as! UIColor
+        subtitle.setting.backgroundColor = config?["backgroundColor"] as! UIColor
+        subtitle.setting.backgroundAlpha = Float(config?["backgroundAlpha"] as! Double)
+        subtitle.setting.strokeColor = config?["strokeColor"] as! UIColor
  
  
         if(subtitleSettingCallBack != nil){
         if(subtitleSettingCallBack != nil){
-            subtitleSettingCallBack!(subtitleSetting)
+            subtitleSettingCallBack!(subtitle)
         }
         }
      
      
     }
     }
@@ -250,10 +250,10 @@ class BFSubtitleSettingView: UIView {
         sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
         sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
         sender.layer.borderWidth = 1.5
         sender.layer.borderWidth = 1.5
         
         
-        subtitleSetting.subtitlePoint = sender.tag
+        subtitle.setting.subtitlePoint = sender.tag
     
     
         if(subtitleSettingCallBack != nil){
         if(subtitleSettingCallBack != nil){
-            subtitleSettingCallBack!(subtitleSetting)
+            subtitleSettingCallBack!(subtitle)
         }
         }
     }
     }
     @objc func wordSizeSetting(sender:UIButton){
     @objc func wordSizeSetting(sender:UIButton){
@@ -267,15 +267,15 @@ class BFSubtitleSettingView: UIView {
         sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
         sender.layer.borderColor = UIColor.hexColor(hexadecimal: "#28BE67").cgColor
         sender.layer.borderWidth = 1.5
         sender.layer.borderWidth = 1.5
 
 
-            if(sender.tag == 0 && subtitleSetting.subtitleSize > 20){
-                subtitleSetting.subtitleSize -= 10
-            }else if(sender.tag == 1 && subtitleSetting.subtitleSize < 120){
-                subtitleSetting.subtitleSize += 10
+            if(sender.tag == 0 && subtitle.setting.subtitleSize > 20){
+                subtitle.setting.subtitleSize -= 10
+            }else if(sender.tag == 1 && subtitle.setting.subtitleSize < 120){
+                subtitle.setting.subtitleSize += 10
             }else{
             }else{
                 BFLog(message: "设置字号超出范围!")
                 BFLog(message: "设置字号超出范围!")
             }
             }
             if(subtitleSettingCallBack != nil){
             if(subtitleSettingCallBack != nil){
-                subtitleSettingCallBack!(subtitleSetting)
+                subtitleSettingCallBack!(subtitle)
             }
             }
             
             
         
         
@@ -284,37 +284,12 @@ class BFSubtitleSettingView: UIView {
     @objc func switchChange(uiswitch:UISwitch) {
     @objc func switchChange(uiswitch:UISwitch) {
         print("字幕开关状态\(uiswitch.isOn )")
         print("字幕开关状态\(uiswitch.isOn )")
         
         
-        subtitleSetting.subtitleIsShow = !uiswitch.isOn
+        subtitle.setting.subtitleIsShow = !uiswitch.isOn
         
         
         if(subtitleSettingCallBack != nil){
         if(subtitleSettingCallBack != nil){
-            subtitleSettingCallBack!(subtitleSetting)
+            subtitleSettingCallBack!(subtitle)
         }
         }
     }
     }
 }
 }
 
 
-public class BFSubtitileSettingModel: NSObject {
-    
-    //字幕样式 值说明0 1 2 3 4 5 6
-    public var subtitleStyle:Int = 0
-    //字幕位置 值说明 0 下 1 中 2 上
-    public var subtitlePoint:Int = 0
-    
-    //字幕文字大小 70px
-    public var subtitleSize:Int = 70
-    
-    //字幕是否显示
-    public var subtitleIsShow:Bool = true
-    
-    //字色
-    public var fontColor: UIColor = .white
-    //背景色
-    public var backgroundColor:UIColor = .clear
-    //背景透明度
-    public var backgroundAlpha:Float = 1.0
-    //描边颜色
-    public var strokeColor: UIColor = .white
-    //描边宽度 6.0
-    public var strokeWidth: CGFloat = 6.0
- 
-    
-}
+