|
Text to Speech
Set Volume:
Set Speed Rate:
Set Pitch of the Sound:
Click the play button to read this text. User above slide bar to adjust the sound.
点击播放键,朗读这句中文。
$.fn.readText = function(options){
var id = $(this);
var setting = $.extend({volume:1,rate:1,pitch:1,lang:'en-US',
playClass:'fa-play-circle',pauseClass:'fa-pause-circle',cancelClass:'fa-stop-circle'},options);
if($(this).data('read')) setting.txt = $('#'+$(this).data('read')).text();
else setting.txt = $(this).parent().text();
if($(this).data('lang')) setting.lang = $(this).data('lang');
if($(this).data('volume')) setting.volume = $(this).data('volume');
if($(this).data('rate')) setting.rate = $(this).data('rate');
if($(this).data('pitch')) setting.pitch = $(this).data('pitch');
id.css({'color':"#333"});
id.switchClass(setting.playClass,setting.cancelClass,0);
var msg = new SpeechSynthesisUtterance();
var voices = window.speechSynthesis.getVoices();
msg.lang = setting.lang;
msg.voiceURI = 'native';
msg.volume = setting.volume; // 0 to 1
msg.rate = setting.rate; // 0.1 to 10
msg.pitch = setting.pitch; //0 to 2
msg.text = setting.txt;
msg.onpause = function(){
id.data('status','paused');
}
msg.onend = function(e) {
id.switchClass(setting.cancelClass,setting.playClass,0);
id.data('status','play');
id.css({'color':"#ccc"});
};
if(speechSynthesis.speaking) {
speechSynthesis.cancel();
if(window.speakingID != $(this).attr('id')){
speechSynthesis.speak(msg);
window.speakingID = $(this).attr('id');
}
}
else {
speechSynthesis.speak(msg);
window.speakingID = $(this).attr('id');
}
}
|
