Tutorial
Using the Web Speech API in Android Trusted Web Activities
September 24, 2026 · 5 min read
Adding voice capabilities to an Android application typically requires writing complex native Java or Kotlin integration code, managing native audio buffers, and linking external libraries. However, because a Trusted Web Activity (TWA) is backed directly by the system's Chrome engine, web developers can leverage the native Web Speech API. This API allows for speech-to-text recognition and text-to-speech synthesis directly within the web codebase, running seamlessly inside the packaged Android application container.
Web Speech API Architecture in Android
The Web Speech API consists of two primary, distinct interfaces: SpeechSynthesis (Text-to-Speech) and SpeechRecognition (Speech-to-Text). Inside an Android TWA, these web APIs do not rely on a custom browser-based implementation. Instead, Chrome forwards these calls to the underlying Android operating system's default speech services, which are typically powered by Google Speech Services.
This tight integration ensures that the speech synthesis voices sound natural and match the user's system language settings, while speech recognition benefits from the same high-accuracy machine learning models used by system-level voice dictation.
Implementing Speech Synthesis
Speech synthesis allows your application to speak text content aloud. This is useful for accessibility utilities, workout apps, language learning tools, and hands-free notifications. The implementation is highly lightweight and does not require any special Android permissions.
The following approach outlines how to construct and execute a speech synthesis request:
const utterance = new SpeechSynthesisUtterance('Welcome to your application.');
const voices = window.speechSynthesis.getVoices();
utterance.voice = voices.find(voice => voice.lang === 'en-GB') || voices[0];
utterance.rate = 1.0;
utterance.pitch = 1.0;
window.speechSynthesis.speak(utterance);
When implementing synthesis, you must handle system-level interruptions. On mobile devices, phone calls, native alarms, or other media playback will pause or terminate the audio output. Listening to the onpause, onerror, and onend events on the utterance instance allows your application to gracefully handle UI state changes when playback is interrupted.
Implementing Speech Recognition
Speech recognition converts spoken audio from the device microphone into plain text strings. Because speech recognition accesses hardware sensors, it requires stricter handling of permissions and lifecycle events than speech synthesis.
To implement speech recognition within your web code, utilize the webkitSpeechRecognition constructor, which is the current vendor-prefixed implementation used by Chromium-based engines on Android:
const recognition = new webkitSpeechRecognition();
recognition.continuous = false;
recognition.interimResults = false;
recognition.lang = 'en-GB';
recognition.onstart = () => { console.log('Microphone listening'); };
recognition.onerror = (event) => { console.error('Speech error:', event.error); };
recognition.onresult = (event) => { const text = event.results[0][0].transcript; console.log('Recognised text:', text); };
recognition.start();
Managing Microphone Permissions
To capture audio for speech recognition, your application must gain access to the device microphone. In a standard web browser, calling recognition.start() prompts the user with a standard browser permission dialogue. In a packaged TWA, this experience should be as native as possible.
Permissions in a TWA are handled via permission delegation. When your TWA application is built, the Android wrapper delegates permission requests directly to Chrome. If the user has already granted microphone permissions to your application via the native Android settings, the TWA will automatically approve the web-level microphone request without showing a browser prompt to the user.
To ensure this delegation runs smoothly, verify that your Android wrapper includes the standard permission declarations in its manifest file:
- android.permission.RECORD_AUDIO: Required to allow the browser engine to capture audio streams.
- android.permission.INTERNET: Required because Google Speech Services often processes complex speech-to-text translation on remote cloud servers rather than locally on the device hardware.
Best Practices for Android Execution
When deploying speech-enabled TWAs, there are specific performance considerations unique to the mobile platform environment:
| Feature | Android TWA Behaviour | Developer Recommendation |
|---|---|---|
| Offline Mode | Fails if local voice packs are not downloaded. | Implement error handling for the network-error state and provide text fallbacks. |
| Background Execution | Microphone input is suspended when the app is paused. | Stop recognition in response to page visibility changes to save battery. |
| System Volatility | Speech volume is controlled by the Media audio stream. | Advise users to check media volume levels if speech output seems inaudible. |
By relying on the native Web Speech API, you avoid inflating the size of your APK or AAB file with massive audio libraries, keeping your download size minimal while offering a fully integrated, voice-capable Android application.
Ready to ship your Android app?
Paste your PWA URL, get a signed APK and a Google Play ready AAB in minutes.
Build my app