2016-10-24 57 views
1

在網頁中,我想要一個女性的聲音說出我的文字。我試圖通過下面的代碼來做到這一點。但現在仍然有男性的聲音在說話。我怎樣才能安排女性的聲音來談我的文字?任何人都可以與我分享我在Google Chrome中運行的正確代碼。如何在Google Chrome中通過網絡語音API獲取女性語音

var voices = speechSynthesis.getVoices(); 
var msg = new SpeechSynthesisUtterance("Hello World!"); 
msg.default=false; 
msg.localservice=true; 
msg.lang = "en-GB"; 
msg.voice = voices[3].name; 
speechSynthesis.speak(msg); 

回答

3

以下代碼適用於我。我希望這個對你有用。

var msg = new SpeechSynthesisUtterance(); 
var voices = window.speechSynthesis.getVoices(); 
msg.voice = voices[3]; 
msg.text = "Hello World"; 
speechSynthesis.speak(msg); 

1日嘗試它可能會發出男性的聲音。但在第二次嘗試時(無需刷新),它會發出女性的聲音,並嘗試將其部署到虛擬服務器上,在第一次嘗試中它將像魅力一樣起作用。

+0

https://stackoverflow.com/questions/45877555/female-voice-in-google-chrome-speechsynthesis,我遇到了一些奇怪的事情。 –

1

在Chrome,我做這樣的事情:

<html> 
<head> 
<script> 
    function speak(language, country, preferredNames, text) { 
     var resolvePromise; 
     var promise = new Promise(r => resolvePromise = r); 

     var voices = window.speechSynthesis.getVoices(); 
     if (voices.length == 0) { 
      new Promise(r => { 
       window.speechSynthesis.onvoiceschanged =() => { 
         window.speechSynthesis.onvoiceschanged = null; 
         r(); 
        }; 
      }) 
      .then(() => speak(language, country, preferredNames, text)) 
      .then(resolvePromise); 
     } else { 
      var msg = new SpeechSynthesisUtterance(text); 
      voices.sort((a, b) => { 
       if (language != null) { 
        var matchA = a.lang.indexOf(language + "-") == 0; 
        var matchB = b.lang.indexOf(language + "-") == 0; 
        if (! matchA && ! matchB) return 0; 
        if (! matchA && matchB) return 1; 
        if (! matchB && matchA) return -1; 
       } 
       if (country != null) { 
        var matchA = a.lang.indexOf("-" + country) == a.lang.length - ("-" + country).length; 
        var matchB = b.lang.indexOf("-" + country) == b.lang.length - ("-" + country).length; 
        if (! matchA && ! matchB) return 0; 
        if (! matchA && matchB) return 1; 
        if (! matchB && matchA) return -1; 
       } 
       if (preferredNames != null) { 
        var indexA = voices.length; 
        var indexB = voices.length; 
        preferredNames.forEach((e, i) => { 
         if (indexA == voices.length && a.name.match(e) != null) indexA = i; 
         if (indexB == voices.length && b.name.match(e) != null) indexB = i; 
        }); 
        return indexA - indexB; 
       } 
       return 0; 
      }); 
      if (voices.length > 0) msg.voice = voices[0]; 
      if (language != null) { 
       msg.lang = language; 
       if (country != null) msg.lang += "-" + country; 
      } 
      msg.onend = resolvePromise; 
      window.speechSynthesis.speak(msg); 

      // msg.onend not triggered without call to console.log(msg)? 
      console.log(msg); 
     } 

     return promise; 
    } 

    speak("en", null, [ /Google US English/, /Samantha/, /Fiona/, /Victoria/, /female/i ], "Hello, world.") 
    .then(() => speak("en", null, [ /female/i ], "Hello, world.")) 
    .then(() => speak("en", "US", [ /female/i ], "Hello, world.")) 
    .then(() => speak("en", "US", null, "Hello, world.")) 
    .then(() => speak("en", "GB", [ /\Wmale/i ], "Hello, world.")); 
</script> 
</head> 
<body> 
</body> 
</html> 
0

此代碼爲我工作。

var speakObj = new SpeechSynthesisUtterance(); 
    speakObj.text = text; 
    speakObj.voice = speechSynthesis.getVoices().filter(function(voice) { 
    return voice.name == "Google UK English Female" 

    })[0]; 
    window.speechSynthesis.speak(speakObj); 
0

這發生在我身上,語音在第一次加載頁面時沒有改變。

我找到了適用於我的解決方案。
getVoices()應在文檔準備就緒時觸發。
所以我在這樣的js頂部添加。

$(document).ready(function() { 
     var voices = window.speechSynthesis.getVoices(); 
    })