flutterflutter-packages

How to automatically detect end of speech and trigger API call?


I'm developing a Flutter application that needs to capture voice input and automatically detect when the user has finished speaking, then send the recorded audio to an API for processing.

Here's what I'm trying to achieve:

  1. User starts speaking (triggered by a button press)
  2. App records the audio
  3. App automatically detects when the user has stopped speaking
  4. App stops recording and sends the audio to an API for processing

However, this requires manual stopping of the recording. I need a way to automatically detect when the user has finished speaking.


Solution

  • Yes, you can use the finalResult property from the SpeechRecognitionResult class to determine if the result is considered final by the platform. This boolean flag indicates whether the result is the ultimate or final output.

    _speech.listen(
          onResult: (result) {
            if (result.finalResult) {
              _handleSpeechEnd(result.recognizedWords);
            }
          },
        );
    

    Here is link

    UPD:

    I understand the issue. To address it, you’ll need to implement your own VAD class that tracks the audio levels (decibels) during recording.

    Here’s how you can approach it with flutter_sound:

    1. Create a VAD Class: This class will help you determine if the audio is silent by analyzing the decibel levels of the recorded data.
    2. Implement Silence Detection: Use the package to monitor audio progress and detect silence based on your VAD class.

    void _startSilenceDetection() {
      _recorder.onProgress.listen((event) {
        // Calculate the amplitude from the recording data
        double amplitude = _calculateAmplitudeDecibels(event);
    
        // Use the VAD class to check if the amplitude indicates silence
        if (_vad.isSilent(amplitude)) {
          // If silence persists for 1-2 seconds (configurable), take the necessary action
        }
      });
    }