lundi 2 septembre 2019

How to use text-to-speech to access a random element of an array?

I am aiming for this to be suitable for a website that uses just HTML and Javascript (CSS too of course, but no J Query or any of the like). I wanted to set up a basic button that when clicked would be accessing an array randomly and having Google's text-to-speech engine turn that randomly accessed element spoken out loud. There are questions concerning randomly accessing arrays, but nothing concerning doing so and having it speak those out loud.

I found two snippets of code that could be applied (I presume) to the array I have provided, but I am not certain how to connect the text-to-speech snippet/s to be able to have my randomly accessed element spoken. Here is what I have found:

var audio = new Audio();
audio.src ='http://translate.google.com/translate_tts?ie=utf-8&tl=en&
q=Hello%20World.';
audio.play();
<script src='https://code.responsivevoice.org/responsivevoice.js';
</script>

<input onclick="responsiveVoice.speak('This is the text you want to
speak');" type='button' value='🔊 Play' />

Either one would do if I can link them as I said.

I had found this on another question and it works to randomly access an element from an array using a button. As such, I only need to figure out how to use text-to-speech, which I know very little about.

var quotes=Array('one', 'two', 'three', 'four' , 'five', 'six' ,
'seven', 'eight');
var Loadquotes= function(){
var newquotes = quotes[Math.floor(Math.random() * quotes.length)];
document.getElementById('quoteshere').innerHTML=newquotes;
};

<button id="getquotes" value="Quotes"
onclick="Loadquotes();">Quotes</button>
<p id="quoteshere" ></p>

I expect to be able to press a button and have any one of the elements in my array randomly selected and spoken out loud by either the google text-to-speech or the responsiveVoice text-to-speech. I do not know how to link these. Would I the position be in any way relative to the speech engine?




Aucun commentaire:

Enregistrer un commentaire