在按鈕上單擊我想blue sky用en-US語言說
此代碼導致utterThis.voice在線錯誤,
請幫助
var synth = window.speechSynthesis;
$('button').on('click', function(){
var utterThis = new SpeechSynthesisUtterance('blue sky');
utterThis.rate = 1;
utterThis.pitch = 1;
utterThis.voice = {voiceURI: 'Google US English', name: 'Google US English', lang: 'en-US', localService: false, default: false};
synth.speak(utterThis);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>CLICK</button>
uj5u.com熱心網友回復:
您需要使用回傳的聲音之一.getVoices- 將普通物件分配給.voice不起作用。
$('button').on('click', function() {
var utterThis = new SpeechSynthesisUtterance('blue sky');
utterThis.rate = 1;
utterThis.pitch = 1;
for (const v of speechSynthesis.getVoices()) {
if (v.voiceURI === 'Google US English') {
utterThis.voice = v;
}
}
speechSynthesis.speak(utterThis);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button>CLICK</button>
回傳的語音由.getVoices瀏覽器/作業系統驗證以支持,并且顯然與將文本轉換為語音的底層服務相關聯 - 另一方面,普通物件不會,這就是為什么普通物件不會不行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/334629.html
標籤:javascript 查询 文字转语音 网络接口
