Skip to content

Commit

Permalink
web speech api
Browse files Browse the repository at this point in the history
  • Loading branch information
stoyan committed Mar 13, 2018
1 parent 0a1d589 commit d614f8f
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/App.js
Expand Up @@ -33,6 +33,16 @@ const dictionary = {


const iOS = !!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform);
let webvoices = null;
if (
'SpeechSynthesisUtterance' in window &&
'speechSynthesis' in window &&
'onvoiceschanged' in speechSynthesis
) {
speechSynthesis.onvoiceschanged = () => {
webvoices = speechSynthesis.getVoices().filter(v => v.lang === 'it-IT' && v.localService);
}
}

let term;

Expand Down Expand Up @@ -74,7 +84,10 @@ function getAnswer(i) {
);
}

function getAudio() {
function getAudio() {
if (webvoices){
return [];
}
const word = justLetters(term[0]);
return [
new Audio(`italiano/voices/Alice/${word}.mp3`),
Expand Down Expand Up @@ -143,7 +156,14 @@ class App extends Component {
say() {
this.pause();
this.setState({pause: false});
this.state.audio[Math.floor(Math.random() * this.state.audio.length)].play();
if (webvoices) {
const u = new SpeechSynthesisUtterance(term[0]);
u.voice = webvoices[Math.floor(Math.random() * webvoices.length)];
speechSynthesis.speak(u);
} else {
this.state.audio[Math.floor(Math.random() * this.state.audio.length)].play();
}

}

toggleSettings() {
Expand Down

0 comments on commit d614f8f

Please sign in to comment.