Fix duplicate recognition issue in _recognitionTimer
Critical fix for recognition logic to prevent duplicate processing:
1. Problem Identified:
- _recognitionTimer was repeatedly calling decode() on same audio data
- Same recognition results were being sent multiple times to UI
- Caused redundant processing and potential performance issues
2. Solution Implemented:
- Add _lastRecognizedText state variable to track previous results
- Only send recognition results when text content actually changes
- Reset _lastRecognizedText when starting new recording session
3. Logic Changes:
- Enhanced recognition loop with duplicate detection:
A command-line utility for Dart development.
Usage: dart <command|dart-file> [arguments]
Global options:
-v, --verbose Show additional command output.
--version Print the Dart SDK version.
--enable-analytics Enable analytics.
--disable-analytics Disable analytics.
--suppress-analytics Disallow analytics for this `dart *` run without changing the analytics configuration.
-h, --help Print this usage information.
Available commands:
analyze Analyze Dart code in a directory.
compile Compile Dart to various formats.
create Create a new Dart project.
devtools Open DevTools (optionally connecting to an existing application).
doc Generate API documentation for Dart projects.
fix Apply automated fixes to Dart source code.
format Idiomatically format Dart source code.
info Show diagnostic information about the installed tooling.
pub Work with packages.
run Run a Dart program.
test Run tests for a project.
Run "dart help <command>" for more information about a command.
See https://dart.dev/tools/dart-tool for detailed documentation.
- Added debug logging for skipped duplicate results
- Reset state on startListening() to ensure clean slate
4. Benefits:
- Eliminates duplicate recognition results sent to UI
- Reduces unnecessary computation and network overhead
- Improves user experience with cleaner, non-repetitive updates
- Better resource utilization and battery life
This fix addresses the core issue where the recognition timer was
processing the same audio stream content repeatedly, ensuring each
unique recognition result is only sent once to the application.
This commit is contained in:
parent
e961996ec6
commit
75080d0c0d
|
|
@ -221,6 +221,7 @@ class YxAsrService implements SpeechRecognitionService {
|
|||
bool _isStartingRecording = false; // 防抖保护:防止重复启动录音
|
||||
bool _isInitialized = false;
|
||||
String _currentModelPath = '';
|
||||
String _lastRecognizedText = ''; // 记录上次识别的文本,避免重复发送
|
||||
|
||||
// 识别速度配置
|
||||
RecognitionSpeed _recognitionSpeed = RecognitionSpeed.fast;
|
||||
|
|
@ -513,6 +514,7 @@ class YxAsrService implements SpeechRecognitionService {
|
|||
await _startAudioRecording(_sampleRate.hz);
|
||||
|
||||
_isListening = true;
|
||||
_lastRecognizedText = ''; // 重置上次识别的文本
|
||||
_statusController.add(true);
|
||||
|
||||
// 开始识别循环处理
|
||||
|
|
@ -740,13 +742,20 @@ class YxAsrService implements SpeechRecognitionService {
|
|||
final result = _recognizer!.getResult(_stream!);
|
||||
debugPrint('🔍 [YxAsr] 获取识别结果: "${result.text}"');
|
||||
|
||||
if (result.text.isNotEmpty && partialResults) {
|
||||
// 只有当识别结果不为空、启用了部分结果、且与上次结果不同时才发送
|
||||
if (result.text.isNotEmpty &&
|
||||
partialResults &&
|
||||
result.text != _lastRecognizedText) {
|
||||
debugPrint('🎤 [YxAsr] 发送实时识别结果: ${result.text}');
|
||||
_lastRecognizedText = result.text; // 更新最后识别的文本
|
||||
_sendResult(
|
||||
recognizedWords: result.text,
|
||||
confidence: 0.8,
|
||||
alternatives: [],
|
||||
);
|
||||
} else if (result.text.isNotEmpty &&
|
||||
result.text == _lastRecognizedText) {
|
||||
debugPrint('🔄 [YxAsr] 跳过重复识别结果: "${result.text}"');
|
||||
}
|
||||
|
||||
// 端点检测已禁用,由用户手动控制录音结束
|
||||
|
|
|
|||
Loading…
Reference in New Issue