Skip to content

Instantly share code, notes, and snippets.

@hiepxanh
Last active May 7, 2024 11:55
Show Gist options
  • Save hiepxanh/de602287ed95fca39a25723297a7dd12 to your computer and use it in GitHub Desktop.
Save hiepxanh/de602287ed95fca39a25723297a7dd12 to your computer and use it in GitHub Desktop.
show list voice and speak vietnamese
var langList = document.getElementById('langList');
if ('speechSynthesis' in window) {
// Start an html table for languages details
var text = '<table border=1><tr><th>Default<th>Language<th>Local<th>Name<th>URI</tr>';
// Get voices; add to table markup
function loadVoices() {
var voices = speechSynthesis.getVoices();
voices.forEach(function(voice, i) {
// Add all details to table
text += '<tr><td>' + voice.default + '<td>'
+ voice.lang + '<td>' + voice.localService
+ '<td>' + voice.name + '<td>' + voice.voiceURI;
});
}
loadVoices();
langList.innerHTML = text;
// Chrome loads voices asynchronously.
window.speechSynthesis.onvoiceschanged = function(e) {
loadVoices();
langList.innerHTML = text;
}
}
var lang = 'vi-VN'
var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.voice = voices.filter(voice => voice.lang == lang)[0];
msg.text = 'Hệ thống đã khởi động, chào mừng Hiệp quay trở lại';
msg.lang = lang;
window.speechSynthesis.speak(msg);
@chithanh1012
Copy link

It is not working right now!

@daothithientamm
Copy link

var msg = new SpeechSynthesisUtterance();
msg.lang = "vi-VN"; 
msg.text = "Xin Chào, tôi là Tâm";
speechSynthesis.speak(msg); 

@DiepLanh
Copy link

DiepLanh commented May 7, 2024

This is awesome tutorial.
I would appreciate if daothithientamn or some experts will show an working example of an HTML file using Javascript with selected voice "vi-VN".
Thank you.

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