XSMessageMehtodChannel.swift
4.26 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//
// XSMessageMehtodChannel.swift
// Runner
//
// Created by MacBook Pro on 2023/6/28.
//
import UIKit
class XSMessageMehtodChannel: NSObject,SSOralEvaluatingManagerDelegate {
var resultData:Dictionary<String, Any>?
var messageChannel:FlutterMethodChannel?
init(message:FlutterBinaryMessenger) {
super.init()
resultData = Dictionary()
messageChannel = FlutterMethodChannel.init(name: "wow_english/sing_sound_method_channely", binaryMessenger: message)
messageChannel!.setMethodCallHandler { call, result in
self.handle(call, result)
}
}
//配置评测信息
func setEvaluateConfig(dict:Dictionary<String, Any>) {
let appKey = dict["appKey"] as? String ?? ""
let secretKey = dict["secretKey"] as? String ?? ""
let userId = dict["userId"] as? String ?? "guest"
let frontTime = dict["frontTime"] as? String ?? "3"
let backTime = dict["frontTime"] as? String ?? "3"
let config = SSOralEvaluatingManagerConfig.init()
config.vad = true
config.isOutputLog = false
config.appKey = appKey //"a418"
config.secretKey = secretKey //"1a16f31f2611bf32fb7b3fc38f5b2c81"'
config.frontTime = Double(frontTime)!
config.backTime = Double(backTime)!
SSOralEvaluatingManager.register(config)
SSOralEvaluatingManager.share().register(.line, userId: userId)
SSOralEvaluatingManager.share().delegate = self
}
//开始评测
func evaluateVioce(dict:Dictionary<String, Any>) {
let text = dict["word"] as? String ?? ""
let type = dict["type"] as? String ?? "0"
let userId = dict["userId"] as? String ?? "guest"
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 handle(_ call: FlutterMethodCall,_ result: @escaping FlutterResult) {
if (call.method == "initVoiceSdk") {
self.setEvaluateConfig(dict:call.arguments as! Dictionary<String, Any>)
return
}
if (call.method == "starVoice") {
self.evaluateVioce(dict: call.arguments as! Dictionary<String, Any>)
return
}
if (call.method == "stopVoice") {
SSOralEvaluatingManager.share().stopEvaluate();
return
}
}
//评测结果回调
func evaluateResult() {
messageChannel!.invokeMethod("voiceResult", arguments: resultData)
}
//SSOralEvaluatingManagerDelegate
/**
评测开始
*/
func oralEvaluatingDidStart() {
print("评测开始")
// messageChannel!.invokeMethod("voiceStart", arguments: nil)
}
/**
评测停止
*/
func oralEvaluatingDidStop() {
print("评测结束")
// messageChannel!.invokeMethod("voiceEnd",arguments: nil)
}
/**
评测完成后的结果
*/
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("评测失败")
messageChannel!.invokeMethod("voiceFail", arguments: error?.localizedDescription)
}
/**
VAD(前置时间)超时回调
*/
func oralEvaluatingDidVADFrontTimeOut() {
print("前置超时--->取消")
SSOralEvaluatingManager.share().cancelEvaluate()
if(resultData?.keys.count == 0) {
resultData!["result"] = "0"
self.evaluateResult();
}
}
/**
VAD(后置时间)超时回调
*/
func oralEvaluatingDidVADBackTimeOut() {
print("后置超时--->结束")
///结束回调
SSOralEvaluatingManager.share().stopEvaluate();
}
}