// // XSMessageMehtodChannel.swift // Runner // // Created by MacBook Pro on 2023/6/28. // import UIKit class XSMessageMehtodChannel: NSObject,SSOralEvaluatingManagerDelegate { var resultData:Dictionary? var messageChannel:FlutterMethodChannel? init(message:FlutterBinaryMessenger) { super.init() resultData = Dictionary() messageChannel = FlutterMethodChannel.init(name: "wow_english/sing_sound_method_channel", binaryMessenger: message) messageChannel!.setMethodCallHandler { call, result in self.handle(call, result) } } //配置评测信息 func setEvaluateConfig(dict:Dictionary) { var appKey = "a418" var secretKey = "c11163aa6c834a028da4a4b30955be99" var service = "wss://api.cloud.ssapi.cn" var userId = "guest" var frontTime = "3" var backTime = "3" if (!dict.keys.isEmpty) { appKey = dict["appKey"] as? String ?? "" secretKey = dict["secretKey"] as? String ?? "" service = dict["service"] as? String ?? "" userId = dict["userId"] as? String ?? "guest" frontTime = dict["frontTime"] as? String ?? "3" backTime = dict["frontTime"] as? String ?? "3" } let config = SSOralEvaluatingManagerConfig.init() config.vad = true config.isOutputLog = false config.appKey = appKey config.secretKey = secretKey config.frontTime = Double(frontTime)! config.backTime = Double(backTime)! config.setValue(service, forKey: "service") SSOralEvaluatingManager.register(config) SSOralEvaluatingManager.share().register(.line, userId: userId) SSOralEvaluatingManager.share().delegate = self } //开始评测 func evaluateVoice(dict:Dictionary) { 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.oralType = .kidSent config.userId = userId SSOralEvaluatingManager.share().startEvaluateOral(with: config) } //开始评测(本地音频文件) func evaluateLocalVoice(dict:Dictionary) { let text = dict["word"] as? String ?? "" let type = dict["type"] as? String ?? "0" let userId = dict["userId"] as? String ?? "guest" let voicePath = dict["voicePath"] as? String ?? "" let config = SSOralEvaluatingConfig() config.oralContent = text if (type == "0") { config.oralType = .word } else { config.oralType = .sentence } config.oralType = .sentence config.userId = userId SSOralEvaluatingManager.share().startEvaluateOral(withWavPath: voicePath, config: config) } func handle(_ call: FlutterMethodCall,_ result: @escaping FlutterResult) { if (call.method == "initVoiceSdk") { self.setEvaluateConfig(dict:call.arguments as! Dictionary) return } if (call.method == "startVoice") { self.evaluateVoice(dict: call.arguments as! Dictionary) return } if (call.method == "startLocalVoice") { self.evaluateLocalVoice(dict: call.arguments as! Dictionary) return } if (call.method == "stopVoice") { SSOralEvaluatingManager.share().stopEvaluate(); return } if (call.method == "cancelVoice") { SSOralEvaluatingManager.share().cancelEvaluate(); 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?) { let resultDict:Dictionary = result as! Dictionary resultData! = resultDict; self.evaluateResult() } /** 评测失败回调 */ func oralEvaluatingDidEndError(_ error: Error?, requestId request_id: String?) { let nsError = error as? NSError var map = Dictionary() map["code"] = nsError?.code map["message"] = error?.localizedDescription messageChannel!.invokeMethod("voiceFail", arguments:map) } /** VAD(前置时间)超时回调 */ func oralEvaluatingDidVADFrontTimeOut() { SSOralEvaluatingManager.share().cancelEvaluate() } /** VAD(后置时间)超时回调 */ func oralEvaluatingDidVADBackTimeOut() { ///结束回调 SSOralEvaluatingManager.share().stopEvaluate(); } }