9

Chrome (33 以降) で新しい音声合成 API を使用して、Web ベースのコミュニケーション支援を作成しています。ユーザーが男性と女性の間で声を変更できるようにしたいと思います。これは、API を使用して行うことができます。ただし、ページが最初にロードされ、(onclick イベントから) 関数が初めて実行されるときは、デフォルトの女性の声が使用されます。その後、実行されるたびに、私が使用しようとしている男性の声が使用されます. どうすれば男性の声も最初から鳴らすことができますか?

JavaScriptを呼び出すボタンは次のとおりです。

<button type="button" name="speakMe"id="speakMe" onclick="speakPhrase($('phraseBar').getValue());" ><img src="images/speakMe.png" /></button>

呼び出されている speakPhrase 関数は次のとおりです。

function speakPhrase(phrase) {
    if(phrase =="")
    {
        alert("Please enter a phrase before asking me to speak for you. Thank you!");
    }
    else
    {
        var speech = new SpeechSynthesisUtterance(phrase);
        var voices = window.speechSynthesis.getVoices();
        speech.voice = voices.filter(function(voice) { return voice.name == 'Google UK English Male'; })[0];
        window.speechSynthesis.speak(speech);

    }
}

誰でも助けることができますか?

4

4 に答える 4

10

最初の呼び出しで voices 配列が空になっているようです。私が読んだことから、それは音声をロードするための非同期呼び出しと関係があります。そのため、最初に呼び出されたときは常に空です。私にとってこれは魔法でした:

var speech_voices;
if ('speechSynthesis' in window) {
  speech_voices = window.speechSynthesis.getVoices();
  window.speechSynthesis.onvoiceschanged = function() {
    speech_voices = window.speechSynthesis.getVoices();
  };
}

音声機能以​​外の場所から呼び出します。

于 2015-07-11T11:38:31.347 に答える
0

少し幸運なことに、音声を設定する前にデフォルト属性をfalseに設定することで問題を解決できました

function speakPhrase(phrase) {
    if(phrase =="")
    {
        alert("Please enter a phrase before asking me to speak for you. Thank you!");
    }
    else
    {
        var speech = new SpeechSynthesisUtterance(phrase);
        var voices = window.speechSynthesis.getVoices();
        speech.default = false;
        speech.voice = voices.filter(function(voice) { return voice.name == 'Google UK English Male'; })[0];
        speech.lang = 'en-GB'; //Also added as for some reason android devices used for testing loaded spanish language 
        window.speechSynthesis.speak(speech);
    }
}
于 2014-04-02T13:15:36.160 に答える