Browse Source

修改讯飞语音识别

weicu
tanlongsheng 10 months ago
parent
commit
56349b4f13
  1. 1457
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt
  2. 57
      local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt

1457
local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/AzureAsrHelper.kt

File diff suppressed because it is too large

57
local_plugins/azure_speech/android/src/main/kotlin/com/yunqiinnovation/azure_speech/XunFeiAsrHelper.kt

@ -4,11 +4,12 @@ import android.content.Context
import android.util.Base64 import android.util.Base64
import android.util.Log import android.util.Log
import okhttp3.* import okhttp3.*
import okio.ByteString import okio.ByteString.Companion.toByteString
import org.json.JSONObject import org.json.JSONObject
import java.net.URLEncoder import java.net.URLEncoder
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
import java.security.MessageDigest import java.security.MessageDigest
import java.util.UUID
import javax.crypto.Mac import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec import javax.crypto.spec.SecretKeySpec
@ -16,77 +17,62 @@ class XunFeiAsrHelper(private val context: Context) {
private val tag = "XunFeiAsrHelper" private val tag = "XunFeiAsrHelper"
private val appId = "43d87d22" private val appId = "43d87d22"
private val accessKeyId = "2bd01ce06ac35957fc0c082f9b6284ec" private val accessKeyId = "83f063755e8d342670002f50a4471d42"
private var webSocket: WebSocket? = null private var webSocket: WebSocket? = null
private val client = OkHttpClient() private val client = OkHttpClient()
// Status: "UNDEFINED", "CONNECTING", "OPEN", "CLOSING", "CLOSED" private var currentSessionId = ""
private var btnStatus = "UNDEFINED"
private var resultText = "" fun start(callback: AzureAsrHelper.ContinuousRecognizeCallback) {
currentSessionId = UUID.randomUUID().toString()
var onResult: ((String) -> Unit)? = null
var onError: ((String) -> Unit)? = null
var onStatusChange: ((String) -> Unit)? = null
fun connectWebSocket() {
val url = getWebSocketUrl() val url = getWebSocketUrl()
val request = Request.Builder().url(url).build() val request = Request.Builder().url(url).build()
changeBtnStatus("CONNECTING")
webSocket = client.newWebSocket(request, object : WebSocketListener() { webSocket = client.newWebSocket(request, object : WebSocketListener() {
override fun onOpen(webSocket: WebSocket, response: Response) { override fun onOpen(webSocket: WebSocket, response: Response) {
Log.d(tag, "WebSocket Opened") Log.d(tag, "WebSocket Opened")
changeBtnStatus("OPEN") callback.onSessionStarted(currentSessionId)
} }
override fun onMessage(webSocket: WebSocket, text: String) { override fun onMessage(webSocket: WebSocket, text: String) {
renderResult(text) renderResult(text, callback)
} }
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) { override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
Log.d(tag, "WebSocket Closing: $code / $reason") Log.d(tag, "WebSocket Closing: $code / $reason")
changeBtnStatus("CLOSING")
webSocket.close(1000, null) webSocket.close(1000, null)
} }
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { override fun onClosed(webSocket: WebSocket, code: Int, reason: String) {
Log.d(tag, "WebSocket Closed: $code / $reason") Log.d(tag, "WebSocket Closed: $code / $reason")
changeBtnStatus("CLOSED") callback.onSessionStopped(currentSessionId)
} }
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) { override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
Log.e(tag, "WebSocket Error", t) Log.e(tag, "WebSocket Error", t)
changeBtnStatus("CLOSED") callback.onError(currentSessionId, 1004, "Xunfei connection failed: ${t.message}")
onError?.invoke(t.message ?: "WebSocket connection failed")
} }
}) })
} }
fun sendAudio(frameBuffer: ByteArray) { fun sendAudio(frameBuffer: ByteArray) {
webSocket?.let { ws -> webSocket?.let { ws: WebSocket ->
ws.send(ByteString.of(frameBuffer, 0, frameBuffer.size)) ws.send(frameBuffer.toByteString(0, frameBuffer.size))
} }
} }
fun close() { fun stop() {
webSocket?.let { ws -> webSocket?.let { ws: WebSocket ->
ws.send("{\"end\": true}") ws.send("{\"end\": true}")
changeBtnStatus("CLOSING")
}
}
private fun changeBtnStatus(status: String) {
btnStatus = status
if (status == "CONNECTING") {
resultText = ""
} }
onStatusChange?.invoke(status)
} }
private fun renderResult(resultData: String) { private fun renderResult(
resultData: String,
callback: AzureAsrHelper.ContinuousRecognizeCallback
) {
try { try {
val jsonData = JSONObject(resultData) val jsonData = JSONObject(resultData)
val action = jsonData.optString("action") val action = jsonData.optString("action")
@ -120,16 +106,15 @@ class XunFeiAsrHelper(private val context: Context) {
} }
if (st.optInt("type") == 0) { if (st.optInt("type") == 0) {
resultText += resultTextTemp callback.onResult(currentSessionId, resultTextTemp, "zh-CN")
onResult?.invoke(resultText)
} else { } else {
onResult?.invoke(resultText + resultTextTemp) callback.onRecognizing(currentSessionId, resultTextTemp, "zh-CN")
} }
} }
"error" -> { "error" -> {
Log.e(tag, "Error: $resultData") Log.e(tag, "Error: $resultData")
onError?.invoke(jsonData.toString()) callback.onError(currentSessionId, 1005, "Xunfei error: $resultData")
} }
} }
} catch (e: Exception) { } catch (e: Exception) {

Loading…
Cancel
Save