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:
Max 2025-09-09 16:58:47 +08:00
parent e961996ec6
commit 75080d0c0d
1 changed files with 10 additions and 1 deletions

View File

@ -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}"');
}
//