Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jlchereau opened this issue Mar 25, 2018 · 3 comments
Open

Comments

@jlchereau
Copy link

jlchereau commented Mar 25, 2018

  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.

@jlchereau jlchereau changed the title This plugin requires an update to conform with Web Speech API This plugin requires an update to conform with the Web Speech API Mar 25, 2018
@macdonst
Copy link
Owner

@jlchereau it is published on npm at https://www.npmjs.com/package/phonegap-plugin-speech-synthesis

  1. Good point, the spec has been updated.
  2. I will see if I can get it to wait until the voice list is populated
  3. Good point, the spec has been updated.
  4. Good point, the spec has been updated.

So thanks for the heads up. I can make some changes to the JS to better align with the spec and publish to NPM.

@macdonst
Copy link
Owner

Fixed #6

@jlchereau
Copy link
Author

jlchereau commented Mar 27, 2018

Thx @macdonst for your very neat plugin. You might also want to check SpeechSynthesisVoice.default as it is always false both on Nexus 2012 with Android 5 and Amazon Tablet with FireOS 5.6, whereas there should be a default voice for each language. See #20.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants