refactor(asr): 优化ASR助手的性能和资源管理

- 使用StringBuilder替代String拼接提升性能
- 清理ASR助手中的监听超时回调避免内存泄漏
- 在各个活动销毁时正确关闭图像解码执行器
- 修复语音识别中间结果的累积逻辑
- 优化ASR会话的资源释放流程
这个提交包含在:
徐勤民 2026-04-21 22:14:44 +08:00
父节点 70ee0c7d64
当前提交 dcf9d51014
共有 5 个文件被更改,包括 9 次插入7 次删除

查看文件

@ -113,7 +113,7 @@ object AsrHelper : OfflineCmdListener {
} }
// 拼接每次识别会话中的中间结果 // 拼接每次识别会话中的中间结果
private var currentPartial = "" private val currentPartial = StringBuilder()
/** 当前页面的场景标识,由各 Activity 在 onResume/onPause 中维护 */ /** 当前页面的场景标识,由各 Activity 在 onResume/onPause 中维护 */
var scene: String = "home" var scene: String = "home"
@ -429,23 +429,20 @@ object AsrHelper : OfflineCmdListener {
} }
override fun onStart(taskId: String) { override fun onStart(taskId: String) {
currentPartial = "" currentPartial.clear()
restartListeningTimeout() 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.append(text)
currentPartial += text
restartListeningTimeout() restartListeningTimeout()
Log.d(TAG, "ASR partial: $text") Log.d(TAG, "ASR partial: $text")
} }
override fun onFinalResult(taskId: String, text: String) { override fun onFinalResult(taskId: String, text: String) {
// 将最终结果追加拼接到会话字符串
isMicRunning = false isMicRunning = false
// 滚动更新当前识别中间结果 currentPartial.append(text)
currentPartial += text
asr?.stopAsrWithMic() asr?.stopAsrWithMic()
Log.d(TAG, "ASR final result: $text") Log.d(TAG, "ASR final result: $text")
dismissListeningDialog() dismissListeningDialog()
@ -590,6 +587,7 @@ object AsrHelper : OfflineCmdListener {
fun close() { fun close() {
isClosed = true isClosed = true
OfflineCmdServiceHelper.removeOnLineListener(this) OfflineCmdServiceHelper.removeOnLineListener(this)
mainHandler.removeCallbacks(listeningTimeoutRunnable)
mainHandler.removeCallbacks(networkCheckRunnable) mainHandler.removeCallbacks(networkCheckRunnable)
mainHandler.removeCallbacks(asrReconnectRunnable) mainHandler.removeCallbacks(asrReconnectRunnable)
mainHandler.removeCallbacks(ttsReconnectRunnable) mainHandler.removeCallbacks(ttsReconnectRunnable)

查看文件

@ -232,6 +232,7 @@ class InspectionResultActivity :
} }
override fun onDestroy() { override fun onDestroy() {
imageDecodeExecutor.shutdown()
super.onDestroy() super.onDestroy()
binding.iv.setImageDrawable(null) binding.iv.setImageDrawable(null)
window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)

查看文件

@ -56,6 +56,7 @@ class SprayingFinishActivity :
} }
override fun onDestroy() { override fun onDestroy() {
imageDecodeExecutor.shutdown()
super.onDestroy() super.onDestroy()
binding.photo1.setImageDrawable(null) binding.photo1.setImageDrawable(null)
binding.photo2.setImageDrawable(null) binding.photo2.setImageDrawable(null)

查看文件

@ -152,6 +152,7 @@ class SprayingOCRActivity :
} }
override fun onDestroy() { override fun onDestroy() {
imageDecodeExecutor.shutdown()
viewModel.cancelUpload() viewModel.cancelUpload()
super.onDestroy() super.onDestroy()
binding.content.setImageDrawable(null) binding.content.setImageDrawable(null)

查看文件

@ -256,6 +256,7 @@ class SprayingResultActivity :
} }
override fun onDestroy() { override fun onDestroy() {
imageDecodeExecutor.shutdown()
uiHandler.removeCallbacksAndMessages(null) uiHandler.removeCallbacksAndMessages(null)
successDialog?.dismiss() successDialog?.dismiss()
super.onDestroy() super.onDestroy()