Getting Started Started with React SpeechToText Component
28 Aug 202514 minutes to read
The SpeechToText component enables users to convert spoken words into text using the Web Speech API. This section explains how to create and configure a SpeechToText component with its available functionalities in a React environment.
Dependencies
The SpeechToText component requires the following Syncfusion packages in your application:
|-- @syncfusion/ej2-react-inputs
|-- @syncfusion/ej2-react-base
|-- @syncfusion/ej2-base
|-- @syncfusion/ej2-buttons
|-- @syncfusion/ej2-popups
Setup for Local Development
To easily set up a React application, use create-vite-app
, which provides a faster development environment, smaller bundle sizes, and optimized builds compared to traditional tools like create-react-app
. For detailed steps, refer to the Vite installation instructions. Vite sets up your environment using JavaScript and optimizes your application for production.
Note: To create a React application using
create-react-app
, refer to this documentation for more details.
To create a new React application, run the following command.
npm create vite@latest my-app
To set-up a React application in TypeScript environment, run the following command.
npm create vite@latest my-app -- --template react-ts
cd my-app
npm run dev
To set-up a React application in JavaScript environment, run the following command.
npm create vite@latest my-app -- --template react
cd my-app
npm run dev
Adding Syncfusion® Packages
All Essential® JS 2 packages are published in the npmjs.com
public registry.
Install the SpeechToText component using the following command:
npm install @syncfusion/ej2-react-inputs --save
Adding SpeechToText Component
Add the SpeechToText component to your application using the <SpeechToTextComponent>
tag directive in the src/App.tsx
file.
[Class-component]
import { SpeechToTextComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from "react-dom";
export default class App extends React.Component<{}, {}> {
public render() {
return (
// specifies the tag for render the Speech to text component
<SpeechToTextComponent id='speechToText'></SpeechToTextComponent>
);
}
}
ReactDOM.render(<App />, document.getElementById('speech-to-text'));
[Functional-component]
import { SpeechToTextComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from "react-dom";
function App() {
return (
// specifies the tag for render the Speech to text component
<SpeechToTextComponent id="speechToText"></SpeechToTextComponent>
);
}
ReactDOM.render(<App />, document.getElementById('speech-to-text'));
Adding CSS Reference
Import the SpeechToText component required CSS references as follows in src/App.css
.
@import "../node_modules/@syncfusion/ej2-base/styles/material.css";
@import "../node_modules/@syncfusion/ej2-buttons/styles/material.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/material.css";
@import "../node_modules/@syncfusion/ej2-react-inputs/styles/material.css";
Run the application
Now run the npm run dev
command in the console to start the development server. This command compiles your code and serves the application locally, opening it in the browser.
npm run dev
The following example shows a basic SpeechToText component.
{ /* Import the SpeechToText.*/ }
import { SpeechToTextComponent, TextAreaComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDom from 'react-dom';
{ /* To render SpeechToText. */ }
export default class App extends React.Component {
textareaObj;
onTranscriptChanged = (args) => {
this.textareaObj.value = args.transcript;
};
render() {
return (
<div id='container'>
<SpeechToTextComponent transcriptChanged={this.onTranscriptChanged.bind(this)}></SpeechToTextComponent>
<TextAreaComponent
id="textareaInst"
ref={(textarea) => {this.textareaObj = textarea} }
resizeMode="None"
rows={5}
cols={50}
value=""
placeholder="Transcribed text will be shown here..."
/>
</div>
);
}
}
ReactDom.render(<App />, document.getElementById('element'));
// Import the SpeechToText.
import { SpeechToTextComponent, TextAreaComponent, TranscriptChangedEventArgs, ButtonSettingsModel } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDom from 'react-dom';
// To render SpeechToText.
function App() {
let textareaObj: TextAreaComponent;
function onTranscriptChanged(args: TranscriptChangedEventArgs) {
textareaObj.value = args.transcript;
}
return (
<div id='container'>
<SpeechToTextComponent transcriptChanged={onTranscriptChanged}></SpeechToTextComponent>
<TextAreaComponent
id="textareaInst"
ref={(textarea) => {textareaObj = textarea} }
resizeMode="None"
rows={5}
cols={50}
value=""
placeholder="Transcribed text will be shown here..."
/>
</div>
);
}
export default App;
ReactDom.render(<App />,document.getElementById('element'));
#container {
margin: 50px auto;
gap: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
/* Represents the styles for loader */
#loader {
color: #008cff;
font-family: 'Helvetica Neue', 'calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}
Important: The SpeechToText component requires an internet connection and a browser that supports SpeechRecognition from the Web Speech API. Speech recognition may request microphone permissions from the user.
Adding Button Content
Customize the button text using the buttonSettings property. Use the content property to display the start listening text and stopContent for the stop listening text.
// Import the SpeechToText.
import { SpeechToTextComponent, TextAreaComponent } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { useRef } from 'react';
// To render SpeechToText.
export default class App extends React.Component {
textareaObj;
onTranscriptChanged = (args) => {
this.textareaObj.value = args.transcript;
};
buttonSettings = {
content: 'Start Listening',
stopContent: 'Stop Listening'
}
render() {
return (
<div id='container'>
<SpeechToTextComponent transcriptChanged={this.onTranscriptChanged.bind(this)} buttonSettings={this.buttonSettings.bind(this)}></SpeechToTextComponent>
<TextAreaComponent
id="textareaInst"
ref={(textarea) => {this.textareaObj = textarea} }
resizeMode="None"
rows={5}
cols={50}
value=""
placeholder="Transcribed text will be shown here..."
/>
</div>
);
}
}
ReactDom.render(<App />,document.getElementById('element'));
// Import the SpeechToText.
import { SpeechToTextComponent, TextAreaComponent, TranscriptChangedEventArgs, ButtonSettingsModel } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { useRef } from 'react';
// To render SpeechToText.
function App() {
let textareaObj: TextAreaComponent;
function onTranscriptChanged(args: TranscriptChangedEventArgs) {
textareaObj.value = args.transcript;
}
const buttonSettings: ButtonSettingsModel = {
content: 'Start Listening',
stopContent: 'Stop Listening'
}
return (
<div id='container'>
<SpeechToTextComponent transcriptChanged={onTranscriptChanged} buttonSettings={buttonSettings}></SpeechToTextComponent>
<TextAreaComponent
id="textareaInst"
ref={(textarea) => {textareaObj = textarea} }
resizeMode="None"
rows={5}
cols={50}
value=""
placeholder="Transcribed text will be shown here..."
/>
</div>
);
}
export default App;
ReactDom.render(<App />,document.getElementById('element'));
#container {
margin: 50px auto;
gap: 20px;
display: flex;
flex-direction: column;
align-items: center;
}
/* Represents the styles for loader */
#loader {
color: #008cff;
font-family: 'Helvetica Neue', 'calibiri';
font-size: 14px;
height: 40px;
left: 45%;
position: absolute;
top: 45%;
width: 30%;
}