-1

Good morning guys, I'm using lib speech_to_text to be able to use the microphone and do voice recognition ... I put a Timer to be able to leave the microphone active for longer after the person speaks, but it only works on iOS, so I saw Android already has a native Timer .... does anyone know what I can do? Thank you!

@action
  onPressedMic({bool continous = false}) {
    this.initSpeech();

    if (this.hasSpeech) {
      try {
        stt.listen(
          localeId: LocaleUtils.getPtBR().localeId,
          onSoundLevelChange: _sttOnSoundLevelChange,
          onResult: _sttResultListener,
          cancelOnError: true,
        );

        // displays the wave indicating the audio capture
        status = StatusFooter.capturing_speech;

        if (Platform.isIOS) _startTimerListen();

        if (_startAudioCapture != null) _startAudioCapture();
      } catch (e) {
        status = StatusFooter.open_speech;
        print("Pressed Mic error: $e");
      }
    }
  }

  @computed
  bool get canShowSuggestions => (this.suggestionChips?.length ?? 0) > 0;

  @action
  _sttResultListener(SpeechRecognitionResult result) async {
    // restart timer, if stt stop command has not been issued
    if (Platform.isIOS && !result.finalResult) _startTimerListen();

    this.textCaptureAudio = result.recognizedWords;

    // sends the question, as the stt stop command has been issued
    if (result.finalResult && this.textCaptureAudio.trim().isNotEmpty)
      this.sendQuestion(this.textCaptureAudio);
  }

void _startTimerListen() {
    _cancelTimerListen();

    timerListen = Timer(Duration(seconds: 3), () {
      if (this.textCaptureAudio.trim().isNotEmpty) {
        stt.stop();
      } else {
        _defineOpenSpeechStatus();
      }
    });
  }

1 Answers1

0

As far as I know there is really no way for an app to extend the period of time the microphone is listening on Android, not with Flutter and not with native Android development. I tried solving this problem for a own app a few years ago but the speech recognition on Android just does not support this. I am sorry but I hope I could help clarifying things.

Stefan
  • 352
  • 2
  • 17