Vue利用Web?Speech?API實(shí)現(xiàn)文字朗讀功能
在 Vue 頁面中實(shí)現(xiàn)文字朗讀(Text-to-Speech,TTS)可以通過瀏覽器的 Web Speech API 實(shí)現(xiàn)。以下是完整實(shí)現(xiàn)方案:
一、使用 Web Speech API 實(shí)現(xiàn)文字朗讀
1. 基本實(shí)現(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 實(shí)例
utterance: null, // 當(dāng)前朗讀實(shí)例
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;
// 加載可用語音列表(需要時(shí)間初始化)
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)建新的朗讀實(shí)例
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ù)、停止操作
語音選擇:自動(dòng)加載系統(tǒng)可用語音(需用戶交互后生效)
參數(shù)調(diào)節(jié):實(shí)時(shí)調(diào)整語速、音調(diào)、音量
狀態(tài)管理:通過 isSpeaking 控制按鈕狀態(tài)
兼容性處理:自動(dòng)檢測瀏覽器支持情況
三、注意事項(xiàng)
1.瀏覽器兼容性
- 支持 Chrome、Firefox、Edge 等現(xiàn)代瀏覽器
- 不支持 iOS 的 WKWebView(需特殊處理)
2.用戶交互要求
首次調(diào)用 speak() 必須由用戶點(diǎn)擊等手勢觸發(fā)(瀏覽器安全策略)
3.語音加載延遲
語音列表 (voices) 可能在頁面加載后需要時(shí)間初始化,建議添加加載狀態(tài)提示
4.移動(dòng)端限制
安卓設(shè)備可能需要用戶明確授權(quán)麥克風(fēng)權(quán)限(即使只是朗讀)
四、高級擴(kuò)展方案
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" />
五、最佳實(shí)踐建議
提供備選方案:對不支持 SpeechSynthesis 的瀏覽器顯示文字提示
加載狀態(tài)反饋:添加 loading 動(dòng)畫等待語音列表加載
錯(cuò)誤處理:監(jiān)聽 onerror 事件并提示用戶
持久化配置:使用 localStorage 保存用戶選擇的語音和參數(shù)
根據(jù)需求選擇原生 API 方案(輕量簡單)或第三方服務(wù)(支持更多語言和音色)。
到此這篇關(guān)于Vue利用Web Speech API實(shí)現(xiàn)文字朗讀功能的文章就介紹到這了,更多相關(guān)Vue Web Speech API文字朗讀內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3實(shí)現(xiàn)對自定義組件自由拖動(dòng)效果
在數(shù)據(jù)可視化看板開發(fā)中,組件的自由拖拽功能能極大提升交互體驗(yàn),本文將基于Vue3生態(tài),從零開始解析如何實(shí)現(xiàn)一個(gè)支持自由拖拽的容器組件,感興趣的朋友一起看看吧2025-04-04
關(guān)于Vue3父子組件emit參數(shù)傳遞問題(解決Vue2this.$emit無效問題)
相信很多人在利用事件驅(qū)動(dòng)向父組件扔?xùn)|西的時(shí)候,發(fā)現(xiàn)原來最常用的this.$emit咋報(bào)錯(cuò)了,竟然用不了了,下面通過本文給大家分享關(guān)于Vue3父子組件emit參數(shù)傳遞問題(解決Vue2this.$emit無效問題),需要的朋友可以參考下2022-07-07
如何使用vue3簡單實(shí)現(xiàn)WebSocket通信
這篇文章主要給大家介紹了關(guān)于如何使用vue3簡單實(shí)現(xiàn)WebSocket通信的相關(guān)資料,WebSocket是全雙工網(wǎng)絡(luò)通信通信協(xié)議,實(shí)現(xiàn)了客戶端和服務(wù)器的平等對話,任何一方都可以主動(dòng)發(fā)送數(shù)據(jù),需要的朋友可以參考下2023-08-08
vue?當(dāng)中組件之間共享數(shù)據(jù)的實(shí)現(xiàn)方式
這篇文章主要介紹了vue?當(dāng)中組件之間共享數(shù)據(jù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
element-plus的el-table自定義表頭篩選查詢功能實(shí)現(xiàn)
這篇文章主要介紹了element-plus的el-table自定義表頭篩選查詢功能實(shí)現(xiàn),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-07-07
vue項(xiàng)目實(shí)現(xiàn)點(diǎn)擊目標(biāo)區(qū)域之外可關(guān)閉(隱藏)目標(biāo)區(qū)域
這篇文章主要介紹了vue項(xiàng)目實(shí)現(xiàn)點(diǎn)擊目標(biāo)區(qū)域之外可關(guān)閉(隱藏)目標(biāo)區(qū)域,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue中利用iscroll.js解決pc端滾動(dòng)問題
這篇文章主要介紹了vue中利用iscroll.js解決pc端滾動(dòng)問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
vue3中storeToRefs讓store中的結(jié)構(gòu)出來的數(shù)據(jù)也能變成響應(yīng)式(推薦)
這篇文章主要介紹了vue3中storeToRefs讓store中的結(jié)構(gòu)出來的數(shù)據(jù)也能變成響應(yīng)式,本文通過實(shí)例代碼給大家介紹的分需要的朋友可以參考下2024-09-09

