跪拜 Guibai
← All articles
Frontend · Vue.js

Zero-Cost Voice I/O for Vue Apps Using the Browser's Web Speech API

By YoungTree ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Adding voice input or output to a web app usually triggers a procurement conversation. The Web Speech API sidesteps that entirely for the majority of internal tools and simple AI interfaces, provided you handle the HTTPS requirement and browser quirks up front.

Summary

The Web Speech API splits into two modules: SpeechSynthesis for local text-to-speech and SpeechRecognition for cloud-backed speech-to-text. A single Vue component wires both together with start/stop controls, real-time interim results, and state management for loading, listening, and errors. The TTS side runs entirely offline with configurable rate, pitch, and volume; the STT side requires HTTPS in production and leans on the browser's own recognition service. Common failure modes — overlapping utterances, denied mic permissions, background leaks after component teardown — each get a specific fix. The approach covers most admin panels, AI chat UIs, and assistive-input features without touching a paid service.

Takeaways
SpeechSynthesis converts text to speech locally with no network calls; cancel any in-flight utterance before starting a new one to prevent overlap.
SpeechRecognition streams real-time transcripts but demands HTTPS in production and fails silently on HTTP.
Setting `continuous: true` and restarting recognition inside `onend` keeps the mic open for multi-turn conversations.
Mic permission denials throw a `not-allowed` error that must be caught; the only recovery path is guiding the user to the browser's site settings.
Call `window.speechSynthesis.cancel()` and `recognition.stop()` inside Vue's `beforeDestroy` to kill background audio and prevent memory leaks.
Browser prefix handling (`window.SpeechRecognition || window.webkitSpeechRecognition`) is still necessary for broader compatibility.
Interim results (`interimResults: true`) let the UI display partial transcripts before the user stops speaking, which feels far more responsive.
Conclusions

Most teams reach for a cloud TTS/STT SDK by default, but the browser's built-in pipeline already covers the accuracy and latency needs of internal tools, demos, and simple AI chat UIs.

The real deployment friction isn't the API itself — it's the HTTPS requirement for `SpeechRecognition`, which catches out teams testing on staging servers without TLS.

Continuous recognition that auto-restarts in `onend` is a small detail that makes the difference between a demo and something usable in a real conversation flow.

The article's pitfall list doubles as a checklist for production readiness: overlapping speech, orphaned background processes, and silent permission failures are exactly what turns a prototype brittle.

Concepts & terms
SpeechSynthesis
A browser API that converts text into spoken audio entirely on-device, with no network requests. Controllable via rate, pitch, volume, and voice selection.
SpeechRecognition
A browser API that captures microphone audio and returns transcribed text. It relies on the browser vendor's cloud speech service, so it requires an internet connection and HTTPS (except on localhost).
interimResults
A SpeechRecognition option that delivers partial, real-time transcription results as the user speaks, rather than waiting until the utterance ends.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗