|
|
|
@ -27,7 +27,10 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
private var sessionId = "" |
|
|
|
private var lastIntermediateResult = "" |
|
|
|
|
|
|
|
fun start(callback: AzureAsrHelper.ContinuousRecognizeCallback) { |
|
|
|
private var currentLanguage = "zh-CN" |
|
|
|
private var isAutoDetectLanguage = false |
|
|
|
|
|
|
|
fun start(callback: AzureAsrHelper.ContinuousRecognizeCallback, language: String, isAutoDetect: Boolean) { |
|
|
|
try { |
|
|
|
webSocket?.cancel() |
|
|
|
webSocket = null |
|
|
|
@ -38,6 +41,9 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
currentSessionId = UUID.randomUUID().toString() |
|
|
|
lastIntermediateResult = "" |
|
|
|
|
|
|
|
currentLanguage = language |
|
|
|
isAutoDetectLanguage = isAutoDetect |
|
|
|
|
|
|
|
val url = getWebSocketUrl() |
|
|
|
val request = Request.Builder().url(url).build() |
|
|
|
|
|
|
|
@ -59,8 +65,17 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
override fun onClosed(webSocket: WebSocket, code: Int, reason: String) { |
|
|
|
Log.d(tag, "WebSocket Closed: $code / $reason") |
|
|
|
if (currentSessionId.isNotEmpty() && lastIntermediateResult.isNotEmpty()) { |
|
|
|
callback.onResult(currentSessionId, lastIntermediateResult, "zh-CN") |
|
|
|
currentSessionId = "" |
|
|
|
val detectedLanguage = if (isAutoDetectLanguage) { |
|
|
|
if (lastIntermediateResult.contains(Regex("[\\u4e00-\\u9fa5]"))) { |
|
|
|
"zh-CN" |
|
|
|
} else { |
|
|
|
"en-US" |
|
|
|
} |
|
|
|
} else { |
|
|
|
currentLanguage |
|
|
|
} |
|
|
|
callback.onResult(currentSessionId, lastIntermediateResult, detectedLanguage) |
|
|
|
currentSessionId = "" |
|
|
|
} |
|
|
|
callback.onSessionStopped(currentSessionId) |
|
|
|
} |
|
|
|
@ -144,8 +159,18 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
return |
|
|
|
} |
|
|
|
|
|
|
|
val detectedLanguage = if (isAutoDetectLanguage) { |
|
|
|
if (resultTextTemp.contains(Regex("[\\u4e00-\\u9fa5]"))) { |
|
|
|
"zh-CN" |
|
|
|
} else { |
|
|
|
"en-US" |
|
|
|
} |
|
|
|
} else { |
|
|
|
currentLanguage |
|
|
|
} |
|
|
|
|
|
|
|
if (st.optInt("type") == 0) { |
|
|
|
callback.onResult(currentSessionId, resultTextTemp, "zh-CN") |
|
|
|
callback.onResult(currentSessionId, resultTextTemp, detectedLanguage) |
|
|
|
currentSessionId = "" |
|
|
|
lastIntermediateResult = "" |
|
|
|
} else { |
|
|
|
@ -154,7 +179,7 @@ class XunFeiAsrHelper(private val context: Context) { |
|
|
|
callback.onSessionStarted(currentSessionId) |
|
|
|
} |
|
|
|
lastIntermediateResult = resultTextTemp |
|
|
|
callback.onRecognizing(currentSessionId, resultTextTemp, "zh-CN") |
|
|
|
callback.onRecognizing(currentSessionId, resultTextTemp, detectedLanguage) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|