Vue利用Web?Speech?API實現(xiàn)文字朗讀功能
在 Vue 頁面中實現(xiàn)文字朗讀(Text-to-Speech,TTS)可以通過瀏覽器的 Web Speech API 實現(xiàn)。以下是完整實現(xiàn)方案:
一、使用 Web Speech API 實現(xiàn)文字朗讀
1. 基本實現(xiàn)
<template> <div> <!-- 輸入要朗讀的文字 --> <textarea v-model="text" rows="3"></textarea> <!-- 控制按鈕 --> <button @click="speak" :disabled="isSpeaking">朗讀</button> <button @click="pause" :disabled="!isSpeaking">暫停</button> <button @click="resume" :disabled="isSpeaking">繼續(xù)</button> <button @click="stop">停止</button> <!-- 語音選擇(可選) --> <select v-model="selectedVoice" v-if="voicesLoaded"> <option v-for="voice in voices" :key="voice.name" :value="voice"> {{ voice.name }} ({{ voice.lang }}) </option> </select> <!-- 參數(shù)調(diào)節(jié)(可選) --> <div> 語速: <input type="range" v-model="rate" min="0.5" max="2" step="0.1"> 音調(diào): <input type="range" v-model="pitch" min="0" max="2" step="0.1"> 音量: <input type="range" v-model="volume" min="0" max="1" step="0.1"> </div> <!-- 兼容性提示 --> <div v-if="!isSupported" class="error"> 當(dāng)前瀏覽器不支持文字朗讀功能,請使用 Chrome/Firefox/Edge 最新版 </div> </div> </template> <script> export default { data() { return { text: "歡迎使用文字朗讀功能", // 朗讀文本 isSpeaking: false, // 朗讀狀態(tài) synth: null, // SpeechSynthesis 實例 utterance: null, // 當(dāng)前朗讀實例 voices: [], // 可用語音列表 voicesLoaded: false, // 語音是否加載完成 selectedVoice: null, // 選擇的語音 rate: 1, // 語速 (0.1-10) pitch: 1, // 音調(diào) (0-2) volume: 1 // 音量 (0-1) }; }, computed: { // 檢查瀏覽器支持情況 isSupported() { return 'speechSynthesis' in window; } }, mounted() { if (this.isSupported) { this.synth = window.speechSynthesis; // 加載可用語音列表(需要時間初始化) this.synth.onvoiceschanged = () => { this.voices = this.synth.getVoices(); this.voicesLoaded = true; this.selectedVoice = this.voices.find(v => v.default) || this.voices[0]; }; } }, methods: { // 創(chuàng)建新的朗讀實例 createUtterance() { const utterance = new SpeechSynthesisUtterance(this.text); utterance.voice = this.selectedVoice; utterance.rate = this.rate; utterance.pitch = this.pitch; utterance.volume = this.volume; // 監(jiān)聽狀態(tài)變化 utterance.onstart = () => this.isSpeaking = true; utterance.onend = utterance.onerror = () => { this.isSpeaking = false; this.utterance = null; }; return utterance; }, // 開始朗讀 speak() { if (!this.text) return; this.stop(); // 停止當(dāng)前朗讀 this.utterance = this.createUtterance(); this.synth.speak(this.utterance); }, // 暫停 pause() { this.synth.pause(); }, // 繼續(xù) resume() { this.synth.resume(); }, // 停止 stop() { this.synth.cancel(); this.isSpeaking = false; } }, beforeDestroy() { this.stop(); } }; </script> <style> .error { color: red; margin-top: 10px; } </style>
二、功能特性
完整控制:支持開始、暫停、繼續(xù)、停止操作
語音選擇:自動加載系統(tǒng)可用語音(需用戶交互后生效)
參數(shù)調(diào)節(jié):實時調(diào)整語速、音調(diào)、音量
狀態(tài)管理:通過 isSpeaking 控制按鈕狀態(tài)
兼容性處理:自動檢測瀏覽器支持情況
三、注意事項
1.瀏覽器兼容性
- 支持 Chrome、Firefox、Edge 等現(xiàn)代瀏覽器
- 不支持 iOS 的 WKWebView(需特殊處理)
2.用戶交互要求
首次調(diào)用 speak() 必須由用戶點擊等手勢觸發(fā)(瀏覽器安全策略)
3.語音加載延遲
語音列表 (voices) 可能在頁面加載后需要時間初始化,建議添加加載狀態(tài)提示
4.移動端限制
安卓設(shè)備可能需要用戶明確授權(quán)麥克風(fēng)權(quán)限(即使只是朗讀)
四、高級擴展方案
1. 使用第三方 TTS 服務(wù)(如阿里云、Azure)
// 示例:調(diào)用阿里云TTS API async function cloudTTS(text) { const response = await fetch('https://tts-api.aliyun.com/synthesize', { method: 'POST', body: JSON.stringify({ text, voice: 'xiaoyun' }) }); const audioBlob = await response.blob(); const url = URL.createObjectURL(audioBlob); new Audio(url).play(); }
2. 使用 vue-speech 插件
npm install vue-speech
import VueSpeech from 'vue-speech'; Vue.use(VueSpeech); // 組件中使用 <vue-speech v-model="text" :voices="voices" />
五、最佳實踐建議
提供備選方案:對不支持 SpeechSynthesis 的瀏覽器顯示文字提示
加載狀態(tài)反饋:添加 loading 動畫等待語音列表加載
錯誤處理:監(jiān)聽 onerror 事件并提示用戶
持久化配置:使用 localStorage 保存用戶選擇的語音和參數(shù)
根據(jù)需求選擇原生 API 方案(輕量簡單)或第三方服務(wù)(支持更多語言和音色)。
到此這篇關(guān)于Vue利用Web Speech API實現(xiàn)文字朗讀功能的文章就介紹到這了,更多相關(guān)Vue Web Speech API文字朗讀內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue?filters和directives訪問this的問題詳解
這篇文章主要介紹了vue?filters和directives訪問this的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01Vue如何利用flex布局實現(xiàn)TV端城市列表功能
用vue開發(fā)了三四個組件了,都是H5的,現(xiàn)在來看看PC是如何玩轉(zhuǎn)組件的,下面這篇文章主要給大家介紹了關(guān)于Vue如何利用flex布局實現(xiàn)TV端城市列表功能的相關(guān)資料,需要的朋友可以參考下2023-01-01vue項目element-ui級聯(lián)選擇器el-cascader回顯的問題及解決
這篇文章主要介紹了vue項目element-ui級聯(lián)選擇器el-cascader回顯的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07vant IndexBar實現(xiàn)的城市列表的示例代碼
這篇文章主要介紹了vant IndexBar實現(xiàn)的城市列表的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11