google-chrome
  • speech-synthesis
  • 2017-08-25 85 views 0 likes 
    0

    我在這兩種情況下都使用了這個確切的代碼。谷歌鍍鉻語音中的女聲合成

     var msg = new SpeechSynthesisUtterance(); 
         var voices = window.speechSynthesis.getVoices(); 
         msg.voice = voices[1]; 
         msg.text = "hello world"; 
         msg.lang = 'en-US'; 
         speechSynthesis.speak(msg); 
    

    如果我在鍍鉻控制檯運行這個,我會得到一個女性的聲音。但是,如果我將精確的代碼放在index.html中並運行它,它會發揮男性的聲音。請問任何人請澄清爲什麼會出現這種差異。提前致謝。

    回答

    0

    找到根本原因。 Getting the list of voices in speechSynthesis of Chrome (Web Speech API)

    調用是異步的,所以當我嘗試在index.html中運行時,voices數組是空的。正如我建議,當我運行這個,然後使用說話它工作正常。

    var msg; 
        var voices; 
        var timer = setInterval(function() { 
         voices = speechSynthesis.getVoices(); 
         console.log(voices); 
         if (voices.length !== 0) { 
          msg = new SpeechSynthesisUtterance(); 
          msg.voice = voices[0]; 
          speechSynthesis.speak(msg); 
          msg.lang = 'en-US'; 
          clearInterval(timer); 
         } 
        }, 200); 
        timer(); 
        speechSynthesis.speak("hello world"); 
    
    相關問題