.NET MAUI SpeechToTextButton Events
The .NET MAUI SpeechToTextButton emits a set of events that allow you to configure the component's behavior in response to speech recognition.
The .NET MAUI SpeechToTextButton exposes the following events:
-
SpeechRecognized—Raised when the speech recognition is successful and the recognized text is available. TheSpeechRecognizedevent handler receives two parameters:- The
senderargument which is of typeobjectbut can be cast toRadSpeechToTextButton. - A
SpeechRecognizerSpeechRecognizedEventArgsargument which has a reference to the:FullText(string) property that contains the current full text recognized from the speech input from the beginning of the current listening session.FullTextConfidenceScoreproperty that indicates the confidence level of the recognition. The value is between 0 and 1, indicating how confident the speech-to-text transcription is. If the value is -1, a confidence score could not be provided.
- The
-
ErrorOccurred—Raised when an error occurs during the speech recognition process. TheErrorOccurredevent handler receives two parameters:- The
senderargument which is of typeobjectbut can be cast toRadSpeechToTextButton. - A
SpeechRecognizerErrorOccurredEventArgsargument which has a reference to the:Message(string) property that contains the error message describing the issue that occurred during speech recognition.Exception(System.Exception) property that contains the exception associated with the speech recognizer error, if any.Handled(bool) property that determines whether the error has been handled. Set this totrueto prevent the default error handling behavior.
- The
-
StateChanged—Raised when the state of the speech recognizer changes. TheStateChangedevent handler receives two parameters:- The
senderargument which is of typeobjectbut can be cast toRadSpeechToTextButton. - An
System.EventArgs.
- The
Example
Here is an example using the SpeechRecognized and ErrorOccurred events:
1. Define the SpeechToTextButton in XAML:
<Grid ColumnDefinitions="*, Auto"
RowDefinitions="Auto">
<Editor x:Name="editor"
AutoSize="TextChanges" />
<telerik:RadSpeechToTextButton x:Name="speechToTextButton"
ErrorOccurred="OnErrorOccurred"
SpeechRecognized="OnSpeechRecognized"
Grid.Column="1"
VerticalOptions="Start" />
</Grid>
2. Add the telerik namespace:
xmlns:telerik="http://schemas.telerik.com/2022/xaml/maui"
3. Handle the SpeechRecognized event:
private void OnSpeechRecognized(object sender, Telerik.Maui.SpeechRecognizer.SpeechRecognizerSpeechRecognizedEventArgs e)
{
this.editor.Text = e.FullText;
}
4. Handle the ErrorOccurred event:
private void OnErrorOccurred(object sender, Telerik.Maui.SpeechRecognizer.SpeechRecognizerErrorOccurredEventArgs e)
{
e.Handled = true;
var error = $"{e.Message}; {e.Exception}";
Application.Current.Windows[0].Page.DisplayAlert("Error", error, "OK");
}
For a runnable example with the SpeechToTextButton Events scenario, see the SDKBrowser Demo Application and go to the SpeechToTextButton > Features category.