feat(asr): 添加语音识别超时机制
- 添加 30 秒监听超时常量配置 - 实现监听超时后的自动停止功能 - 添加超时回调和日志记录机制 - 在开始录音时启动超时计时器 - 在部分结果返回时重置超时时间 - 在停止录音时取消超时任务
这个提交包含在:
父节点
50e8358796
当前提交
8a83ec8054
@ -56,10 +56,18 @@ object AsrHelper : OfflineCmdListener {
|
|||||||
private var isTtsConnected = false
|
private var isTtsConnected = false
|
||||||
|
|
||||||
private const val WAKE_RESPONSE = "在呢,您请说"
|
private const val WAKE_RESPONSE = "在呢,您请说"
|
||||||
|
private const val LISTENING_TIMEOUT_MS = 30_000L
|
||||||
|
|
||||||
private val mainHandler = Handler(Looper.getMainLooper())
|
private val mainHandler = Handler(Looper.getMainLooper())
|
||||||
private var listeningDialog: AlertDialog? = null
|
private var listeningDialog: AlertDialog? = null
|
||||||
private var noNetworkDialog: AlertDialog? = null
|
private var noNetworkDialog: AlertDialog? = null
|
||||||
|
private val listeningTimeoutRunnable = Runnable {
|
||||||
|
if (!isMicRunning) return@Runnable
|
||||||
|
runCatching { asr?.stopAsrWithMic() }
|
||||||
|
isMicRunning = false
|
||||||
|
dismissListeningDialog()
|
||||||
|
Log.d(TAG, "ASR listening timeout after ${LISTENING_TIMEOUT_MS}ms")
|
||||||
|
}
|
||||||
private val networkCheckRunnable = object : Runnable {
|
private val networkCheckRunnable = object : Runnable {
|
||||||
override fun run() {
|
override fun run() {
|
||||||
if (isNetworkAvailable()) {
|
if (isNetworkAvailable()) {
|
||||||
@ -141,6 +149,7 @@ object AsrHelper : OfflineCmdListener {
|
|||||||
Log.w(TAG, "ASR startMic ignored: mic already running")
|
Log.w(TAG, "ASR startMic ignored: mic already running")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
restartListeningTimeout()
|
||||||
runCatching { asr?.startAsrWithMic() }
|
runCatching { asr?.startAsrWithMic() }
|
||||||
.onSuccess {
|
.onSuccess {
|
||||||
isMicRunning = true
|
isMicRunning = true
|
||||||
@ -149,6 +158,15 @@ object AsrHelper : OfflineCmdListener {
|
|||||||
.onFailure { Log.e(TAG, "ASR startAsrWithMic failed: ${it.message}") }
|
.onFailure { Log.e(TAG, "ASR startAsrWithMic failed: ${it.message}") }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun restartListeningTimeout() {
|
||||||
|
mainHandler.removeCallbacks(listeningTimeoutRunnable)
|
||||||
|
mainHandler.postDelayed(listeningTimeoutRunnable, LISTENING_TIMEOUT_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun cancelListeningTimeout() {
|
||||||
|
mainHandler.removeCallbacks(listeningTimeoutRunnable)
|
||||||
|
}
|
||||||
|
|
||||||
private fun showListeningDialog() {
|
private fun showListeningDialog() {
|
||||||
mainHandler.post {
|
mainHandler.post {
|
||||||
listeningDialog?.dismiss()
|
listeningDialog?.dismiss()
|
||||||
@ -185,6 +203,7 @@ object AsrHelper : OfflineCmdListener {
|
|||||||
listeningDialog?.dismiss()
|
listeningDialog?.dismiss()
|
||||||
listeningDialog = null
|
listeningDialog = null
|
||||||
}
|
}
|
||||||
|
cancelListeningTimeout()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun showNoNetworkDialog() {
|
private fun showNoNetworkDialog() {
|
||||||
@ -247,12 +266,14 @@ object AsrHelper : OfflineCmdListener {
|
|||||||
|
|
||||||
override fun onStart(taskId: String) {
|
override fun onStart(taskId: String) {
|
||||||
currentPartial = ""
|
currentPartial = ""
|
||||||
|
restartListeningTimeout()
|
||||||
Log.d(TAG, "ASR started: $taskId")
|
Log.d(TAG, "ASR started: $taskId")
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onPartialResult(taskId: String, text: String) {
|
override fun onPartialResult(taskId: String, text: String) {
|
||||||
// 滚动更新当前识别中间结果
|
// 滚动更新当前识别中间结果
|
||||||
currentPartial += text
|
currentPartial += text
|
||||||
|
restartListeningTimeout()
|
||||||
Log.d(TAG, "ASR partial: $text")
|
Log.d(TAG, "ASR partial: $text")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
正在加载...
在新工单中引用
屏蔽一个用户