2016-10-24 22 views
1

ウェブページでは、女性の声でテキストを発声します。私はこれをコードに従って実行しようとしました。しかし、今でも男性の声が話しています。私のテキストを話すために女性の声をどのように手配できますか?誰でも私にGoogle Chromeで動作する正しいコードを教えてもらえますか?Google ChromeのWeb Speech 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回では、男性の声を出すかもしれません。しかし、2回目の試み(爽やかではない)では、女性の声を出してダミーサーバーに展開しようとすると、そこには最初の魅力のように機能します。 Chromeで

+0

https://stackoverflow.com/questions/45877555/female-voice-in-google-chrome-speechsynthesis、私はいくつかの奇妙な私が出会った。 –

1

は、私はこのような何かを:

<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(); 
    }) 
関連する問題