I have the below .Net MAUI code that works great.
I want to add continuous speech, I mean when I speak, the recorder must not stop until I press stop button.
In the below code, when I stop speaking, or I pause for like 2-3 sec, it stops itself, so I don't want it to behave like that.
I want it to continue recording even if i pause my speech for 2-3 sec.
public class SpeechToTextImplementation : ISpeechToText
{
public event EventHandler<SpeechToTextRecognitionResultUpdatedEventArgs> RecognitionResultUpdated = null!;
public event EventHandler<SpeechToTextRecognitionResultCompletedEventArgs> RecognitionResultCompleted = null!;
public event EventHandler<SpeechToTextStateChangedEventArgs> StateChanged = null!;
public SpeechToTextState CurrentState { get; private set; } = SpeechToTextState.Idle;
public async Task<SpeechToTextResult> ListenAsync(CultureInfo culture, IProgress<string>? recognitionResult, CancellationToken cancellationToken)
{
await Task.Delay(2000, cancellationToken);
string finalText = "Recognized text from ListenAsync.";
recognitionResult?.Report(finalText);
return new SpeechToTextResult(finalText, null);
}
public async Task StartListenAsync(CultureInfo culture, CancellationToken cancellationToken)
{
await Task.Delay(1000, cancellationToken);
CurrentState = SpeechToTextState.Listening;
StateChanged?.Invoke(this, new SpeechToTextStateChangedEventArgs(CurrentState));
Device.StartTimer(TimeSpan.FromSeconds(1), () =>
{
RecognitionResultUpdated?.Invoke(this, new SpeechToTextRecognitionResultUpdatedEventArgs(new SpeechToTextRecognitionResult("Simulated intermediate result.")));
return true;
});
}
public async Task StopListenAsync(CancellationToken cancellationToken)
{
await Task.Delay(500, cancellationToken);
CurrentState = SpeechToTextState.Stopped;
StateChanged?.Invoke(this, new SpeechToTextStateChangedEventArgs(CurrentState));
RecognitionResultCompleted?.Invoke(this, new SpeechToTextRecognitionResultCompletedEventArgs(new SpeechToTextRecognitionResult("Simulated final result.")));
}
public async Task<bool> RequestPermissions(CancellationToken cancellationToken)
{
await Task.Delay(500, cancellationToken);
return true;
}
public ValueTask DisposeAsync()
{
return ValueTask.CompletedTask;
}
}