Sagar Acharya
Sagar Acharya

Reputation: 3767

speech recognition in flutter after voice input parts of the text are making the api call

I am working with the flutter text to speech functionality. Later after when I get the text it is sending the text in parts.

I am using the speech_recognition plugin where I input my voice command and the text is sent to one of the API created, but the problem is it sends the text in parts to the API. Example : Actual Text: What time it is but some times the String goes 2 times 2 API calls OR Some times it sends only a few words at a time


    _speechRecognition.setAvailabilityHandler((bool result) => setState(() {
          _isAvailable = result;
          print('Avalibility handler was called');
        }));

    _speechRecognition.setRecognitionStartedHandler(
      () => setState(() {
        _isListening = true;
        print('recognition start  handler was called');
      }),
    );

    _speechRecognition.setRecognitionResultHandler(
      (String speech) => setState(() {
        resultText = speech;

        print(
            'set result handler was called  This is the result handler : $resultText');
        if (_isListening == false && resultText != '') {
          _handleSubmitted(speech);


        }
      }),
    );
    _speechRecognition.setRecognitionCompleteHandler(() {
      setState(() {
        print('set recognition complete handler was called');
        _isListening = false;
      });
    });

    _speechRecognition.activate().then(
          (result) => setState(() {
            _isAvailable = result;
            print('set activate handler was called');
          }),
        );
  }```

Upvotes: 2

Views: 2461

Answers (1)

Ankit Mahadik
Ankit Mahadik

Reputation: 2435

What i have done to resolve this issue in my project i have taken one new variable to hold result of speech recognition and on complete method i have passed that result to API call Example:-

String transcription = '';

_speech.setRecognitionResultHandler(onRecognitionResult);
_speech.setRecognitionCompleteHandler(onRecognitionComplete);

void onRecognitionResult(String text) {
    setState(() {
      transcription = text;
    });
  }


void onRecognitionComplete() {
    setState(() {
      LogUtils.d("result.....$transcription");
     _handleSubmitted(transcription);
    });
  }

Instead of managing API call in onRecognitionResult use onRecognitionComplete

Upvotes: 2

Related Questions