Blame view

ios/Runner/VoiceXSMessageChannel.swift 3.32 KB
2eb67dd4   liangchengyou   feat:调整代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
  //
  //  VoiceXSMessageChannel.swift
  //  Runner
  //
  //  Created by MacBook Pro on 2023/6/28.
  //
  
  import UIKit
  
  class VoiceXSMessageChannel: NSObject,SSOralEvaluatingManagerDelegate {
      var resultData:Dictionary<String, Any>?
      var channel:FlutterBasicMessageChannel?
      init(messager:FlutterBinaryMessenger) {
          super.init()
          resultData = Dictionary()
          self.setEvaluateConfig()
          channel = FlutterBasicMessageChannel(name: "com.owEnglish.voiceXs.BasicMessageChannel", binaryMessenger: messager)
          channel!.setMessageHandler { message, reply in
              if let dict = message as? Dictionary<String, Any> {
                  self.evaluateVioce(dict: dict);
              }
          }
      }
      
      //配置评测信息
      func setEvaluateConfig() {
          let config = SSOralEvaluatingManagerConfig.init()
          config.appKey = "a418"
          config.secretKey = "1a16f31f2611bf32fb7b3fc38f5b2c81"
          config.vad = true
          config.frontTime = 3
          config.backTime = 3
          config.isOutputLog = false
          SSOralEvaluatingManager.register(config)
          SSOralEvaluatingManager.share().register(.line, userId: "321")
          SSOralEvaluatingManager.share().delegate = self
      }
      
      //开始评测
      func evaluateVioce(dict:Dictionary<String, Any>)  {
          let text = dict["word"] as! String
          let type = dict["type"] as! Int
          let userId = dict["userId"] as! String
          let config = SSOralEvaluatingConfig()
          config.oralContent = text
          if (type == 0) {
              config.oralType = .word
          } else {
              config.oralType = .sentence
          }
          config.userId = userId
          SSOralEvaluatingManager.share().startEvaluateOral(with: config)
      }
      
      //评测结果回调
      func evaluateResult() {
          channel!.sendMessage(resultData) {(reply) in
              self.resultData?.removeAll()
          }
      }
      
      //SSOralEvaluatingManagerDelegate
      /**
       评测开始
       */
      func oralEvaluatingDidStart() {
          print("评测开始")
      }
      
      /**
       评测停止
       */
      func oralEvaluatingDidStop() {
          print("评测结束")
      }
      
      /**
       评测完成后的结果
       */
      func oralEvaluatingDidEnd(withResult result: [AnyHashable : Any]?, requestId request_id: String?) {
          print("评测完成结果")
          let resultDict:Dictionary<String, Any> = result?["result"] as! Dictionary
          resultData!["result"] = "1"
          //分数
          resultData!["overall"] = resultDict["overall"]
          self.evaluateResult()
      }
      
      /**
       评测失败回调
       */
      func oralEvaluatingDidEndError(_ error: Error?, requestId request_id: String?) {
          print("评测失败")
          resultData!["result"] = "0"
          self.evaluateResult()
      }
      
      /**
       VAD(前置时间超时回调
       */
      func oralEvaluatingDidVADFrontTimeOut() {
          print("前置超时--->取消")
          SSOralEvaluatingManager.share().cancelEvaluate()
          if(resultData?.keys.count == 0) {
              resultData!["result"] = "0"
              self.evaluateResult();
          }
      }
      
      /**
       VAD(后置时间超时回调
       */
      func oralEvaluatingDidVADBackTimeOut() {
          print("后置超时--->结束")
          ///结束回调
          SSOralEvaluatingManager.share().stopEvaluate();
      }
  }