目前h5新增一個文字轉語音的功能(但是正在完善中,勉強能用),h5新增的SpeechSynthesisUtterance實體
首先new一個SpeechSynthesisUtterance物件
使用實體物件的一些屬性,包括:
text– 要合成的文字內容,字串,lang– 使用的語言,字串, 例如:"zh-cn"voiceURI– 指定希望使用的聲音和服務,字串,volume– 聲音的音量,區間范圍是0到1,默認是1,rate– 語速,數值,默認值是1,范圍是0.1到10,表示語速的倍數,例如2表示正常語速的兩倍,pitch– 表示說話的音高,數值,范圍從0(最小)到2(最大),默認值為1,
方法
- speak() 將對應的實體添加到語音佇列中
- cancel() 洗掉佇列中所有的語音.如果正在播放,則直接停止
- pause() 暫停語音
- resume() 恢復暫停的語音
- getVoices 獲取支持的語言陣列. 注意:必須添加在voiceschanged事件中才能生效
但是這個方法不支持老版的ie,需要加瀏覽器判斷方法
isIe(){ if(!!window.ActiveXObject || "ActiveXObject " in window){ return true }else{ return false } },
然后根據ie使用方法
voice(e){ window.speechSynthesis.cancel() let timer timer = setInterval(() => { let msg = e if(this.isIe()){ let voiceObj = new ActiveXObject("Sapi.SpVoice") voiceObj.Rate = -1 // 語速 voiceObj.Volume = 50 // 音量 voiceObj.Speak(msg,1) }else{ let speakMsg = new SpeechSynthesisUtterance(msg) speakMsg.rate = 1 // 語速 speakMsg.pitch = 3 // 音量 window.speechSynthesis.speak(speakMsg) } }, 1000) setTimeout(() => { // 一段時間后清除定時器 clearInterval(timer) }, 1000) },
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/504479.html
標籤:其他
上一篇:常見的網頁復制粘貼禁用問題
