Skip to content

This plugin requires an update to conform with the Web Speech API #18

Open
@jlchereau

Description

@jlchereau
  1. SpeechSynthesisVoiceList is not part of the Web Speech API: getVoices should return an array of voices:
var voices = [];
function loadVoices () {
    voices = window.speechSynthesis.getVoices() || [];
    if (voices._list) {
        voices = voices._list;
    }
}
  1. getVoices are loaded asynchronously

The first time it is called, window.speechSynthesis.getVoices() returns a number and voices_list is undefined;
The second time it is called window.speechSynthesis.getVoices() returns a SpeechSynthesisVoiceList.
Tested on Andorid Nexus 2012 tablet with Android 5 and Amazon Tablet with FireOS 5.6
I think this is because Phonegap now uses Chrome Web View and Chrome loads voices asynchronously.

Therefore you need:

function onDeviceReady () {
    if ('speechSynthesis' in window && $.isFunction (window.speechSynthesis.getVoices)) {
        loadVoices();
        if ('onvoiceschanged' in window.speechSynthesis) {
            // Chrome loads voices asynchronously
            window.speechSynthesis.onvoiceschanged = loadVoices;
        } else {
            // We need to attempt to load twice especially for https://github.com/macdonst/SpeechSynthesisPlugin
            setTimeout(loadVoices, 3000);
        }
    }
}
  1. SpeechSynthesisUtterance should be updated to take voice and not voiceURI and lang
if ('voice' in utterance) {
    // Standard Web Speech API
    utterance.voice = voice; // This sets the language
    // Setting an unavailable language in Microsoft Edge breaks the speech,
    // but hopefully we got a SpeechSynthesisVoice
    // utterance.lang = language;
} else {
    // For https://github.com/macdonst/SpeechSynthesisPlugin
    utterance.voiceURI = voice.voiceURI;
    utterance.lang = voice.lang;
}
  1. See SpeechSynthesisUtterance constructor takes an argument #6

It would also be nice to publish this plugin on npm especially to avoid it being listed each time you check npm outdated.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions