2017-03-06 11 views
0

"Microsoft Zira Desktop - English(United States)"などの名前でSpeechSynthesisUtteranceボイスを設定する必要があります。SpeechSynthesisUtteranceを名前で設定する

私は利用可能な音声のドロップダウンを使用して、ユーザーが希望する「音声」を選択できるようにするコードを用意しています。そのボイス名は、$_SESSION[voicesettings][voice]のPHPセッション変数(およびクッキー)に保存されるので、ユーザーは次回の訪問時に希望する音声を得ることができます。

function loadVoices() { 
    // Fetch the available voices. 
    var voices = speechSynthesis.getVoices(); 
    var voiceselected = "<?php echo $_SESSION[voicesettings][voice]?>"; 
    // Loop through each of the voices. 
    voices.forEach(function(voice, i) { 
    // Create a new option element. 
     var option = document.createElement('option'); 
    // Set the options value and text. 
     option.value = voice.name; 
     option.innerHTML = voice.name; 
     if (voice.name === voiceselected){ 
      option.selected = true; 
    } 
    // Add the option to the voice selector. 
     voiceSelect.appendChild(option); 
    }); 
} 

// Execute loadVoices. 
loadVoices(); 

// Chrome loads voices asynchronously. 
window.speechSynthesis.onvoiceschanged = function(e) { 
    loadVoices(); 
}; 

私はセット(リコール/保存)セッション変数にユーザーが選択したボイス名をできるように、コードのこの部分は、作品(コードではありません。ここで

は、使用されるコードの抽出物であります示されている)。

これで、SpeechSynthesisUtteranceオブジェクトを作成する別のページで、保存した値(音声)を使用する必要があります。しかし、そのオブジェクトの '声'を設定することに問題があります。

これまでのところ、私のような、それの名前で `speechMessage.voiceを設定するにはどうすればよい

var speechMessage = new SpeechSynthesisUtterance(msg); 
    speechMessage.rate = <?php echo $rate;?>; 
    speechMessage.pitch = <?php echo $pitch;?>; 
    speechMessage.volume = <?php echo $volume;?>; 

の「Microsoftジラデスクトップ - 英語(米国)」?

+0

以下のコードを名前で声を見つけることができます://開発者:_thisはSpeechSynthesisVoiceのいずれかに設定する必要がありますが、[SpeechSynthesis.getVoices()](HTTPSによって返されたオブジェクト。 mozilla.org/en-US/docs/Web/API/SpeechSynthesis/getVoices)_。だから、私は 'getVoices()'を呼び出して、あなたの名前と一致する名前を見つけるものと考えます。 –

答えて

0

あなたがドキュメントによると

var voices = window.speechSynthesis.getVoices(); 
var foundVoices = voices.filter(function(voice) 
{ 
    return voice.name == 'Microsoft Zira Desktop - English (United States)'; 
}); 

if(foundVoices.length === 1){ 
    speechMessage.voice = foundVoices[0]; 
} 
関連する問題