JavaScript/jQuery SpeechToText - Getting Started

NOTE
Before you start the tutorial, ensure DevExtreme is installed in your application.

This tutorial guides you through the following steps:

  • Add a SpeechToText to a page.
  • Integrate SpeechToText with a text editor (TextArea).
  • Handle transcription errors.
  • Customize SpeechRecognition options.

Each section in this tutorial covers a single configuration step. You can find the complete source code in the following GitHub repository:

View on GitHub

Create SpeechToText

Add DevExtreme to your jQuery application and use the following code to create a SpeechToText:

index.js
index.html
  • $(function() {
  • $("#speech-to-text").dxSpeechToText({});
  • });
  • <html>
  • <head>
  • <!-- ... -->
  • <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
  • <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/25.2.11/css/dx.fluent.blue.light.css">
  • <script type="text/javascript" src="https://cdn3.devexpress.com/jslib/25.2.11/js/dx.all.js"></script>
  • <script type="text/javascript" src="index.js"></script>
  • </head>
  • <body>
  • <div id="speech-to-text"></div>
  • </body>
  • </html>

Integrate with a Text Editor

SpeechToText returns transcription results in the onResult handler. Final transcriptions are nested within the onResult parameter (a ResultEvent object). To access final transcriptions, implement the following code:

Code
  • onResult(e) {
  • const speechEvent = e.event as SpeechRecognitionEvent;
  • const resultText = Object.values(speechEvent.results)
  • .map((result: unknown) => (result as SpeechRecognitionResult)[0].transcript)
  • .join(' ');
  • },
NOTE

To add all SpeechRecognition types (including SpeechRecognitionEvent) to your project:

  1. Install the @types/dom-speech-recognition package:

    Code
    • npm i -D @types/dom-speech-recognition
  2. Add "dom-speech-recognition" to the types array in your TypeScript configuration:

    tsconfig.app.json
    • {
    • "compilerOptions": {
    • "types": ["dom-speech-recognition"],
    • }
    • }

To integrate the component with a text input (for instance, DevExtreme TextArea), update the input's value in onResult:

index.js
  • $(function() {
  • const textArea = $('#textarea-container').dxTextArea({
  • placeholder: 'Transcribed text will appear here...',
  • }).dxTextArea('instance');
  •  
  • $("#speech-to-text").dxSpeechToText({
  • onResult(e) {
  • const resultText = Object.values(e.event.results)
  • .map((resultItem) => resultItem[0].transcript)
  • .join(' ');
  •  
  • textArea.option('value', resultText);
  • },
  • });
  • });

Handle Errors

SpeechToText returns Web Speech API errors in the onError handler. Error codes are nested within the onError parameter (an ErrorEvent object). To display these errors to users, you can implement the notify utility method:

Code
  • onError(e) {
  • DevExpress.ui.notify(e.event.error);
  • }

This example implements an if statement to display a unique message for not-allowed errors (thrown when users deny microphone permissions) and a common message for other errors:

index.js
  • $(function() {
  • // ...
  •  
  • $("#speech-to-text").dxSpeechToText({
  • onError(e) {
  • if (e.event.error === 'not-allowed') {
  • DevExpress.ui.notify('Microphone access denied. Please grant microphone permissions and try again.', 'error', 5000);
  • } else {
  • DevExpress.ui.notify(`An error occurred during speech recognition: ${e.event.error}`, 'error', 5000);
  • }
  • },
  • });
  • });

Configure SpeechRecognition Options

To configure SpeechRecognition options, define the speechRecognitionConfig object. This example enables continuous recognition (transcription continues after speech ends):

index.js
  • $(function() {
  • // ...
  •  
  • $("#speech-to-text").dxSpeechToText({
  • speechRecognitionConfig: {
  • continuous: true,
  • },
  • });
  • });