Parcourir la source

对接语音点击事件

刘亚军 il y a 1 an
Parent
commit
f4a2f8c107
1 fichiers modifiés avec 48 ajouts et 20 suppressions
  1. 48 20
      CampusAssistant/MainController.swift

+ 48 - 20
CampusAssistant/MainController.swift

@@ -32,7 +32,7 @@ import YJExtensions
     let IMID: Int32 = 33364
 #endif
 
-// WkWebView加载地址
+// WkWebView加载地址  http://m.lancooedu.com/#/login http://isca.lancooedu.com/airobot/AndroidWeb/index.html
 #if DEBUG
     let mainUrl:String = "http://m.lancooedu.com/#/login"
 #else
@@ -45,7 +45,8 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
     var mmkv:MMKV = MMKV.default()!
     var payScheme:String = ""
     let filePath:String = NSHomeDirectory () +  "/Documents/"
-
+    var testBtn: UIButton!
+    var stopTestBtn: UIButton!
     static var preViewFilePath:String = ""
     // 在线消息推送队列 实现堆叠的通知
     let bannerQueueToDisplaySeveralBanners = NotificationBannerQueue(maxBannersOnScreenSimultaneously: 3)
@@ -105,18 +106,31 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
         configuration.userContentController = userContentController
         
         configuration.allowsInlineMediaPlayback = true;
-        
         webView = WKWebView(frame: .zero,configuration: configuration)
         webView.uiDelegate = self
         webView.navigationDelegate = self
         webView.allowsBackForwardNavigationGestures = true
         webView.scrollView.contentInsetAdjustmentBehavior = UIScrollView.ContentInsetAdjustmentBehavior.never
         view = webView
+        
+//        testAction()
+        
         if(mmkv.contains(key: "userInfo")) {
             self.rtcLogin()
         }
     }
-    
+    func testAction() {
+        testBtn = UIButton.init(frame: CGRect(x: 100, y: 200, width: 100, height: 50))
+        testBtn.setTitle("测试识别", for: .normal)
+        testBtn.setTitleColor(UIColor.red, for: .normal)
+        testBtn.addTarget(self, action: #selector(startSpeechMark), for: .touchUpInside)
+        view.addSubview(testBtn)
+        stopTestBtn = UIButton.init(frame: CGRect(x: 100, y: 260, width: 100, height: 50))
+        stopTestBtn.setTitle("停止测试", for: .normal)
+        stopTestBtn.setTitleColor(UIColor.red, for: .normal)
+        stopTestBtn.addTarget(self, action: #selector(stopSpeechMark), for: .touchUpInside)
+        view.addSubview(stopTestBtn)
+    }
     @objc func refuslt(){
              
     }
@@ -214,17 +228,28 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
         print("周期 ---应用程序确实进入了后台")
     }
     
+    //语音识别授权
+    func authSpeechMark(userID:String){
+        YJNetManager.default().userID = userID
+        if (!YJSpeechManager.default().isInitEngine()){
+            YJSpeechManager.default().initEngine()
+            YJSpeechManager.default().updateWarrntIdAuth()
+        }
+    }
+    
     //开启语音识别
-    func startSpeechMark() {
-        YJSpeechManager.default().startEngine(atRefText: nil, markType: YJSpeechMarkType.ASR, symbol: true)
+    @objc func startSpeechMark() {
+        YJSpeechManager.default().startEngine(atRefText: nil, markType: .chineseASR, symbol: true)
         YJSpeechManager.default().speechEngineResult { resultModel in
-            let isError:Bool = ((resultModel?.isError) != nil)
-            if (isError){
+            if (resultModel!.isError){
                 LGAlertHUD.shareInstance().showError(withStatus: resultModel?.errorMsg)
             }else {
-                var answer:String? = resultModel?.recognition.yj_deleteWhitespaceCharacter()
-                if (answer != nil && answer!.count > 0){
-                    
+                let answer:String = resultModel?.recognition.yj_deleteWhitespaceCharacter() ?? ""
+                if (answer.count > 0){
+                    print("识别内容:\(answer)")
+                    self.webView.evaluateJavaScript("onAndroidRecognitionResult('\(answer)')") { obj, error in
+                        print("识别上传错误:",error ?? "无错")
+                    }
                 }else{
                     LGAlertHUD.shareInstance().showInfo(withStatus: "语音输入为空")
                 }
@@ -232,8 +257,8 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
         }
     }
     //停止语音识别
-    func stopSpeechMark() {
-        YJSpeechManager.default().stopEngine(withTip: "语音识别中...")
+    @objc func stopSpeechMark() {
+        YJSpeechManager.default().stopEngine(withTip: NSString.yj_Char1())
     }
     
     // RTC login
@@ -247,13 +272,6 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
             let userName = userInfoDict["UserName"] as! String
             let avatar = userInfoDict["PhotoPath"] as! String
             let userInfo:[String:String] = ["userId":"\(schoolId)_\(userId)","userName":userName,"avatar": avatar]
-            
-            YJNetManager.default().userID = userId as! String
-            if (!YJSpeechManager.default().isInitEngine()){
-                YJSpeechManager.default().initEngine()
-                YJSpeechManager.default().updateWarrntIdAuth()
-            }
-            
             ProfileManager.shared.login(phone: dicValueString(userInfo)!, code: "") { [weak self] in
                 guard let `self` = self else { return }
                 self.loading.stopAnimating()
@@ -291,6 +309,7 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
         }
     }
     
+    
     // RTC authLogin
     func rtcAuthLogin() {
         let userInfo = mmkv.string(forKey: "userInfo",defaultValue: "")!
@@ -298,6 +317,10 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
             // userInfo不为空,转化为字典,取出SchoolID和UserID,拼接出实时音的登录用户名
             let userInfoDict = userInfo.toDictionary()
             let userName = userInfoDict["UserName"] as! String
+            let userId = userInfoDict["UserID"]!
+            
+            authSpeechMark(userID: userId as! String)
+            
             if ProfileManager.shared.autoLogin(success: {
                 self.loginIM { [weak self] (success) in
                     guard let `self` = self else { return }
@@ -694,6 +717,10 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
                     print("开始选择文件")
                     self.present(document, animated:true, completion:nil)
                 }
+            }else if method == "startSpeechRecognition" {
+                startSpeechMark()
+            }else if method == "stopSpeechRecognition" {
+                stopSpeechMark()
             }
         }
      }
@@ -957,6 +984,7 @@ class MainController: UIViewController,WKUIDelegate, WKNavigationDelegate, WKScr
     
     func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
         LoadingView.complete()
+        YJSpeechManager.default().setMicrophoneAuthorization()
     }
     
     func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {