csdcorp / speech_to_text

A Flutter plugin that exposes device specific text to speech recognition capability.
BSD 3-Clause "New" or "Revised" License
351 stars 218 forks source link

_speech.listen method is getting called two times #456

Closed zyfi4472 closed 6 months ago

zyfi4472 commented 6 months ago

my listen method gets called two times,why? It is working fine on an android device but when i try to run it on an iphone, then it behaves like this Please help me out with this issue

my listen method is implemented as follows

void startListening() { setState(() { isMicOn = true; });

String previousText = '';
Timer? debounceTimer;

_speech.listen(
  onResult: (result) {
    print('on result getting called');
    setState(() {
      _text = result.recognizedWords;
    });

    Future.delayed(const Duration(seconds: 3), () {
      _speech.stop();

    });

    print('Recognised word : $_text');

  },
);

}

and my logs are as follows

[plugin] invokeFlutter notifyStatus [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] HypothesizeTranscription [plugin] Encoded JSON result: {"alternates":[{"recognizedWords":"Hello","confidence":0}],"finalResult":false} [plugin] invokeFlutter textRecognition flutter: on result getting called flutter: Recognised word : Hello [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] invokeFlutter soundLevelChange [plugin] Error deactivation: Session deactivation failed [plugin] invokeFlutter notifyStatus [plugin] FinishRecognition true [plugin] Encoded JSON result: {"alternates":[{"recognizedWords":"Hello","confidence":0.96}],"finalResult":true} [plugin] invokeFlutter textRecognition [plugin] FinishSuccessfully flutter: on result getting called flutter: Recognised word : Hello [plugin] invokeFlutter notifyStatus

sowens-csd commented 6 months ago

Do you mean that the onResult callback is getting called twice? If so that's correct. Speech recognizers return both interim and final results, interim results are like work in progress. The recognizer may change those results in subsequent calls.

There is a flag in the results finalResult that you can use to determine whether the recognizer is finished or not. The listen method also has a parameter, partialResults, you can use to determine whether you receive all results or just the final.

zyfi4472 commented 6 months ago

Thank you sir, The issue is resolved.