diff --git a/src/providers/SpeechProvider/SpeechProvider.actions.js b/src/providers/SpeechProvider/SpeechProvider.actions.js index eb3a873a1..671018f8e 100644 --- a/src/providers/SpeechProvider/SpeechProvider.actions.js +++ b/src/providers/SpeechProvider/SpeechProvider.actions.js @@ -57,6 +57,7 @@ export function getVoices() { name })); dispatch(receiveVoices(voices)); + return voices; }); }; } diff --git a/src/providers/SpeechProvider/SpeechProvider.container.js b/src/providers/SpeechProvider/SpeechProvider.container.js index 819e414ff..d8dce5660 100644 --- a/src/providers/SpeechProvider/SpeechProvider.container.js +++ b/src/providers/SpeechProvider/SpeechProvider.container.js @@ -20,11 +20,8 @@ export class SpeechProvider extends Component { render() { const { voices, children } = this.props; - if (!voices.length) { - return null; - } - return React.Children.only(children); + return !!voices.length ? React.Children.only(children) : null; } } @@ -37,4 +34,7 @@ const mapDispatchToProps = { getVoices }; -export default connect(mapStateToProps, mapDispatchToProps)(SpeechProvider); +export default connect( + mapStateToProps, + mapDispatchToProps +)(SpeechProvider); diff --git a/src/providers/SpeechProvider/SpeechProvider.reducer.js b/src/providers/SpeechProvider/SpeechProvider.reducer.js index 3a937cbd0..ccf6bfeb2 100644 --- a/src/providers/SpeechProvider/SpeechProvider.reducer.js +++ b/src/providers/SpeechProvider/SpeechProvider.reducer.js @@ -37,7 +37,7 @@ function speechProviderReducer(state = initialState, action) { return { ...state, - options, + options }; case RECEIVE_VOICES: return { @@ -60,8 +60,9 @@ function speechProviderReducer(state = initialState, action) { options: { ...state.options, lang: action.lang, - voiceURI: state.voices.find(voice => voice.lang === action.lang) - .voiceURI + voiceURI: state.voices.length + ? state.voices.find(voice => voice.lang === action.lang).voiceURI + : null } }; case CHANGE_PITCH: diff --git a/src/providers/SpeechProvider/tts.js b/src/providers/SpeechProvider/tts.js index 1accb408c..e482a5bb0 100644 --- a/src/providers/SpeechProvider/tts.js +++ b/src/providers/SpeechProvider/tts.js @@ -38,8 +38,7 @@ const tts = { // Get voices depending on platform (browser/cordova) _getPlatformVoices() { - const voices = synth.getVoices() || []; - + const voices = synth.getVoices(); // On Cordova, voice results are under `._list` return voices._list || voices; }, @@ -59,10 +58,22 @@ const tts = { // Android if ('onvoiceschanged' in synth) { - speechSynthesis.onvoiceschanged = () => { - cachedVoices = this._getPlatformVoices(); - resolve(this.normalizeVoices(cachedVoices)); - }; + synth.addEventListener('voiceschanged', function voiceslst() { + const voices = synth.getVoices(); + if (!voices.length) { + return null; + } else { + synth.removeEventListener('voiceschanged', voiceslst); + // On Cordova, voice results are under `._list` + cachedVoices = voices._list || voices; + let nVoices = cachedVoices.map(({ voiceURI, name, lang }) => ({ + voiceURI, + name, + lang: normalizeLanguageCode(lang) + })); + resolve(nVoices); + } + }); } }); },