Jesse Dupuy
Jesse Dupuy

Reputation: 1758

Using Speech Synthesis to speak array of strings with pauses between

I'm trying to figure out a solid way to pass an array of strings to the Speech Synthesis API with pauses between speaking each array item. E.g. speak item 1, pause for x seconds, speak item 2, etc.

I tried using the API's onend method/event, but it only works a few times before it stops working entirely, reading off the rest of the array items back to back from that point onward.

Suggestions?

var dropdown = $('#item-select'),
  interval = $('#item-interval').val() * 1000,
  itemBtn = $('#item-btn'),
  stopBtn = $('#item-stop'),
  items = {
    'first': ['hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine', 'hello, sunshine'],
    'second': ['hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world', 'hello, world']
  };

if ('speechSynthesis' in window) {
  function speak(text) {
    var msg = new SpeechSynthesisUtterance();
    msg.text = text;

    speechSynthesis.speak(msg);
    
    msg.addEventListener('end', function(e) {
      speechSynthesis.pause();
      window.setTimeout(function() {
        speechSynthesis.resume();
      }, interval);
    });
  }
  
  itemBtn.on('click', function(evt) {
    currItem = items[dropdown.val()];
    for (var phrase in currItem) {
      speak(currItem[phrase]);
    }
  });
  stopBtn.on('click', function(evt) {
    speechSynthesis.cancel();
  });
} else {
  console.log('Voice synthesis isn\'t supported in your browser.');
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id='item-select'>
  <option selected value='first'>First Item</option>
  <option value='second'>Second Item</option>
</select>
<label for='item-interval'>Interval (seconds)</label>
<input id='item-interval' max='10' name='item-interval' type='range' value='2'>
<button id='item-btn'>Speak!</button>
<button id='item-stop'>CEASE FIRE</button>

Upvotes: 3

Views: 1950

Answers (1)

uncleoptimus
uncleoptimus

Reputation: 380

How about a timeout-delayed recursive trip thru the array of items:

function speak(list) {
    if (list.length) {
      var msg = new SpeechSynthesisUtterance();
      msg.text = list[0];

      speechSynthesis.speak(msg);

      msg.addEventListener('end', function(e) {
        window.setTimeout(() => {
            speak(list.slice(1));
        }, interval);
      });
    }
  }

  itemBtn.on('click', function(evt) {
    const list = items[dropdown.val()];  
    speak(list);
  });

It worked for me but then I encountered some weirdness. At times SpeechSynthesis just STOPPED firing the 'end' event:

This fiddle adds some logs to show when the utterance begins/ends; observe if the 'end' event just ceases for no reason.

https://jsfiddle.net/cak4bju9/2/

This stack post on the buggy behaviour was useful:

SpeechSynthesis API onend callback not working

Upvotes: 3

Related Questions