Methods in EJ2 React Speech To Text

10 Aug 20267 minutes to read

Start Listening

The startListening method initiates speech recognition and begins converting speech to text. This is used to programmatically start the recognition process without user interaction on the microphone button.

Stop Listening

The stopListening method terminates the speech recognition process. This can be used to programmatically stop the recognition when an action is completed.

The following example demonstrates how to use the startListening and stopListening methods in the 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;
    speechToText;
    onTranscriptChanged = (args) => {
        this.textareaObj.value = args.transcript;
    };
    startListening = () => {
        this.speechToText.startListening();
    }
    stopListening = () => {
        this.speechToText.stopListening();
    }
    render() {
        return (
            <div id='container'>
                <div className="actions" >
                    <button id="startListening" className="e-btn" onClick={this.startListening.bind(this)}>Start Listening</button>
                    <button id="stopListening" className="e-btn" onClick={this.stopListening.bind(this)}>Stop Listening</button>
                </div>
                <SpeechToTextComponent ref={(speech) => {this.speechToText = speech} } 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 { SpeechToTextComponent, TextAreaComponent, TranscriptChangedEventArgs } from '@syncfusion/ej2-react-inputs';
import * as React from 'react';
import * as ReactDOM from "react-dom";

function App() {

    const speechToText = React.useRef<SpeechToTextComponent>(null);
    let textareaObj: TextAreaComponent;

    function onTranscriptChanged(args: TranscriptChangedEventArgs) {
        textareaObj.value = args.transcript;
    }
    function startListening() : void {
        speechToText.current.startListening();
    }
    function stopListening() : void{
        speechToText.current.stopListening();
    }
    return (
        <div id='container'>
            <div className="actions" >
                <button id="startListening" className="e-btn" onClick={startListening}>Start Listening</button>
                <button id="stopListening" className="e-btn" onClick={stopListening}>Stop Listening</button>
            </div>
            <SpeechToTextComponent ref={speechToText} 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;
}
.actions {
    display: flex;
    gap: 5px;
}
/* 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%;
}