SpeechToTextButton IntegrationPremium
The KendoReact SpeechToTextButton provides flexible integration options for speech recognition. By default, it uses the browser's native Web Speech API, which provides a seamless out-of-the-box experience. This enables you to pass the transcript to other components, such as a text input, and create powerful voice-enabled user interfaces.
Default Integration with the Web Speech API
The SpeechToTextButton works immediately without any additional configuration—the browser recognition engine starts when the user activates the button. Simply add the component to your template and handle onResult to process recognized speech.
const handleResult = (event: SpeechToTextResultEvent) => {
if (event.alternatives.length > 0) {
setTextAreaValue((prev) => prev + event.alternatives[0].transcript + ' ');
}
};
<SpeechToTextButton onResult={handleResult} />;
The Web Speech API provides:
- Built-in speech recognition without external dependencies
- Support for multiple languages
- Real-time processing
- Interim results capability
- Multiple recognition alternatives
For details on enabling features such as continuous recognition, interim results, and changing the recognition language, refer to the Configuration Options documentation.
TextArea Integration
The following example demonstrates the default Web Speech API integration. It adds the SpeechToTextButton to a TextArea suffix and sends final recognition results to the field.
External Service Integration
For scenarios requiring custom speech recognition providers, you can integrate the component with third-party services by setting the integrationMode prop to none.
This approach is ideal when you want to:
- Use a cloud-based speech recognition service (such as Azure, Google, or AWS).
- Integrate with an on-premises or proprietary speech-to-text engine.
- Implement custom business logic for processing audio input.
How It Works
When integrationMode is set to none, the SpeechToTextButton disables its built-in speech recognition. Instead, you can handle the button's events to implement your own speech recognition logic. Typically, you will:
- Capture audio from the user (using the browser's APIs or a custom solution).
- Send the audio data to your chosen speech-to-text provider.
- Process the provider's response and update your UI accordingly.
When the SpeechToTextButton is used for the first time, the browser will prompt the user for permission to access the microphone. This is required for capturing audio input. Users must grant permission for speech recognition to function.
Integration Flow
Use the onStart and onEnd callbacks, so you can handle audio capture and speech recognition entirely within your own logic.
- Use
onStartto begin capturing audio from the user. - Use
onEndto stop audio capture and send the recorded audio to your chosen speech-to-text provider. - Process the provider's response and update your UI with the recognized text.
All result handling and UI updates should be implemented in your custom event handlers, as the component will not emit recognition results when using third-party or custom integrations.
Implementation Example
The following example integrates the SpeechToTextButton with the Google Cloud Speech-to-Text API by using async event handlers to record audio and send it to the Google Cloud service. To test the example, copy it locally and set your API key to the GOOGLE_API_KEY variable. In production, call the API through a server so the key is not exposed in the browser.
import { useRef, useState } from 'react';
import { SpeechToTextButton } from '@progress/kendo-react-buttons';
const App = () => {
const mediaRecorderRef = useRef<MediaRecorder | null>(null);
const audioChunksRef = useRef<Blob[]>([]);
const [transcript, setTranscript] = useState('');
const GOOGLE_API_KEY = 'YOUR_GOOGLE_API_KEY_HERE'; // Replace with your actual Google API key; in production, call the API through a server so the key is not exposed in the browser
const handleStart = async () => {
setTranscript('');
try {
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
const mediaRecorder = new MediaRecorder(stream);
mediaRecorderRef.current = mediaRecorder;
audioChunksRef.current = [];
mediaRecorder.ondataavailable = (event) => {
if (event.data.size > 0) {
audioChunksRef.current.push(event.data);
}
};
// Transcribe only after the recorder has delivered all audio chunks.
mediaRecorder.onstop = handleAudioStop;
mediaRecorder.start();
console.log('Recording started');
} catch (err) {
console.error('Microphone access error:', err);
}
};
const handleEnd = () => {
if (mediaRecorderRef.current?.state === 'recording') {
mediaRecorderRef.current.stop();
}
console.log('Recording stopped');
};
const handleAudioStop = async () => {
const audioBlob = new Blob(audioChunksRef.current, { type: 'audio/webm' });
const arrayBuffer = await audioBlob.arrayBuffer();
const audioBase64 = btoa(String.fromCharCode(...new Uint8Array(arrayBuffer)));
try {
const response = await fetch(`https://speech.googleapis.com/v1/speech:recognize?key=${GOOGLE_API_KEY}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
config: {
// Must match the browser's MediaRecorder output (WebM Opus at 48 kHz).
encoding: 'WEBM_OPUS',
sampleRateHertz: 48000,
languageCode: 'en-US'
},
audio: {
content: audioBase64
}
})
});
const result = await response.json();
if (result.results && result.results.length > 0) {
const transcriptText = result.results.map((r: any) => r.alternatives[0].transcript).join(' ');
setTranscript(transcriptText);
console.log('Transcription result:', transcriptText);
} else {
console.log('No transcription result');
}
} catch (error) {
console.error('Speech-to-Text API error:', error);
}
};
const handleError = (e: any) => {
console.error('Speech error:', e);
};
return (
<div>
<SpeechToTextButton integrationMode="none" onStart={handleStart} onEnd={handleEnd} onError={handleError} />
<p>
<strong>Transcript:</strong> {transcript}
</p>
</div>
);
};
export default App;
When integrating with third-party or custom speech-to-text providers (
integrationMode="none"), you are responsible for:
- Capturing and recording audio from the user.
- Converting the audio to the format required by your provider (for example, WAV/PCM).
- Handling authentication (such as API keys).
- Sending the audio to the provider's API and processing the response.
Refer to your provider's documentation for details on supported formats and API usage.
Browser Support and Microphone Permissions
Web Speech API support varies by browser and environment. For the latest browser compatibility, refer to the Web Speech API browser compatibility table. The component checks for the SpeechRecognition or webkitSpeechRecognition browser API. If neither exists, it reports an unsupported-browser message through onError when you provide that callback.
The browser controls microphone permission for speech recognition. Handle permission or recognition errors with onError. If your custom integration captures audio with getUserMedia(), run it in a secure context and handle microphone permission separately.