Vue利用Web?Speech?API實(shí)現(xiàn)文字朗讀功能
在 Vue 頁(yè)面中實(shí)現(xiàn)文字朗讀(Text-to-Speech,TTS)可以通過(guò)瀏覽器的 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>
<!-- 語(yǔ)音選擇(可選) -->
<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>
語(yǔ)速: <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)前瀏覽器不支持文字朗讀功能,請(qǐ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: [], // 可用語(yǔ)音列表
voicesLoaded: false, // 語(yǔ)音是否加載完成
selectedVoice: null, // 選擇的語(yǔ)音
rate: 1, // 語(yǔ)速 (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;
// 加載可用語(yǔ)音列表(需要時(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īng)狀態(tài)變化
utterance.onstart = () => this.isSpeaking = true;
utterance.onend = utterance.onerror = () => {
this.isSpeaking = false;
this.utterance = null;
};
return utterance;
},
// 開(kāi)始朗讀
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>
二、功能特性
完整控制:支持開(kāi)始、暫停、繼續(xù)、停止操作
語(yǔ)音選擇:自動(dòng)加載系統(tǒng)可用語(yǔ)音(需用戶交互后生效)
參數(shù)調(diào)節(jié):實(shí)時(shí)調(diào)整語(yǔ)速、音調(diào)、音量
狀態(tài)管理:通過(guò) isSpeaking 控制按鈕狀態(tài)
兼容性處理:自動(dòng)檢測(cè)瀏覽器支持情況
三、注意事項(xiàng)
1.瀏覽器兼容性
- 支持 Chrome、Firefox、Edge 等現(xiàn)代瀏覽器
- 不支持 iOS 的 WKWebView(需特殊處理)
2.用戶交互要求
首次調(diào)用 speak() 必須由用戶點(diǎn)擊等手勢(shì)觸發(fā)(瀏覽器安全策略)
3.語(yǔ)音加載延遲
語(yǔ)音列表 (voices) 可能在頁(yè)面加載后需要時(shí)間初始化,建議添加加載狀態(tài)提示
4.移動(dòng)端限制
安卓設(shè)備可能需要用戶明確授權(quán)麥克風(fēng)權(quán)限(即使只是朗讀)
四、高級(jí)擴(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í)踐建議
提供備選方案:對(duì)不支持 SpeechSynthesis 的瀏覽器顯示文字提示
加載狀態(tài)反饋:添加 loading 動(dòng)畫(huà)等待語(yǔ)音列表加載
錯(cuò)誤處理:監(jiān)聽(tīng) onerror 事件并提示用戶
持久化配置:使用 localStorage 保存用戶選擇的語(yǔ)音和參數(shù)
根據(jù)需求選擇原生 API 方案(輕量簡(jiǎn)單)或第三方服務(wù)(支持更多語(yǔ)言和音色)。
到此這篇關(guān)于Vue利用Web Speech API實(shí)現(xiàn)文字朗讀功能的文章就介紹到這了,更多相關(guān)Vue Web Speech API文字朗讀內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue實(shí)現(xiàn)錄音并轉(zhuǎn)文字功能包括PC端web手機(jī)端web(實(shí)現(xiàn)過(guò)程)
- Vue實(shí)現(xiàn)文字上下滾動(dòng)動(dòng)畫(huà)的示例代碼
- vue實(shí)現(xiàn)搜索并高亮文字的兩種方式總結(jié)
- vue實(shí)現(xiàn)文字檢索時(shí)候?qū)⑺阉鲀?nèi)容標(biāo)紅功能
- vue實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能詳解
- 如何利用Web Speech API之speechSynthesis實(shí)現(xiàn)文字轉(zhuǎn)語(yǔ)音功能
相關(guān)文章
vue?filters和directives訪問(wèn)this的問(wèn)題詳解
這篇文章主要介紹了vue?filters和directives訪問(wèn)this的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01
vue3在router中使用store報(bào)錯(cuò)的完美解決方案
這篇文章主要介紹了vue3在router中使用store報(bào)錯(cuò)解決方案,就是需要在實(shí)例化一下,因?yàn)樵诰幾grouter的時(shí)候pinia還未被實(shí)例化,文中補(bǔ)充介紹了vue3中router和store詳細(xì)使用教程方法,感興趣的朋友一起看看吧2023-11-11
Vue項(xiàng)目中引入ESLint校驗(yàn)代碼避免代碼錯(cuò)誤
這篇文章主要為大家介紹了Vue項(xiàng)目中引入ESLint插件校驗(yàn)代碼避免代碼錯(cuò)誤的使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07
vue實(shí)現(xiàn)可移動(dòng)的懸浮按鈕
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)可移動(dòng)的懸浮按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-03-03
vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換
這篇文章主要為大家詳細(xì)介紹了vue+js點(diǎn)擊箭頭實(shí)現(xiàn)圖片切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
Vue如何利用flex布局實(shí)現(xiàn)TV端城市列表功能
用vue開(kāi)發(fā)了三四個(gè)組件了,都是H5的,現(xiàn)在來(lái)看看PC是如何玩轉(zhuǎn)組件的,下面這篇文章主要給大家介紹了關(guān)于Vue如何利用flex布局實(shí)現(xiàn)TV端城市列表功能的相關(guān)資料,需要的朋友可以參考下2023-01-01
vue項(xiàng)目element-ui級(jí)聯(lián)選擇器el-cascader回顯的問(wèn)題及解決
這篇文章主要介紹了vue項(xiàng)目element-ui級(jí)聯(lián)選擇器el-cascader回顯的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vant IndexBar實(shí)現(xiàn)的城市列表的示例代碼
這篇文章主要介紹了vant IndexBar實(shí)現(xiàn)的城市列表的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11

