Skip to content

Instantly share code, notes, and snippets.

@takatama
Created December 1, 2014 10:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takatama/03337f75216e9261d279 to your computer and use it in GitHub Desktop.
Save takatama/03337f75216e9261d279 to your computer and use it in GitHub Desktop.
Web Speech API (Synthesis)
https://dvcs.w3.org/hg/speech-api/raw-file/tip/speechapi.html
<p>
<input id="input-japanese" type="text" value="こんにちは" />
<button id="speak-japanese">日本語</button>
</p>
<p>
<input id="input-english" type="text" value="Hello world." />
<button id="speak-english">English</button>
</p>
$(function() {
var Speaker = function (rate) {
this.rate = rate || 0.5;
this.speak = function (text, lang) {
var u = new SpeechSynthesisUtterance();
u.rate = this.rate;
u.text = text;
u.lang = lang;
//u.onend = function(event) { alert('Finished in ' + event.elapsedTime + ' seconds.'); }
window.speechSynthesis.speak(u);
};
this.english = function (text) {
this.speak(text, 'en-US');
};
this.japanese = function(text) {
this.speak(text, 'ja-JP');
}
};
var speaker = new Speaker();
$('#speak-japanese').click(function () {
var text = $('#input-japanese').val();
speaker.japanese(text);
});
$('#speak-english').click(function () {
var text = $('#input-english').val();
speaker.english(text);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment