javascript - 语音合成 API 在说话时突出显示单词

标签 javascript html speech-synthesis

目前,我正在制作一个简单的应用程序,其中使用语音合成 API 来朗读文本。我想在说话时突出显示单词(粗体)。我目前有一个非常基本的实现,使用 'onboundary' 事件来执行此操作。但是,我想知道是否有更好/更好的方法,因为我的实现是基于一些假设。

var words;
var wordIdx;
var text;
var utterance = new SpeechSynthesisUtterance();
utterance.lang = 'en-UK';
utterance.rate = 1;

window.onload = function(){
    document.getElementById('textarea').innerText = 'This is a text area.  It is used as a simple test to check whether these words are highlighted as they are spoken using the web speech synthesis API (utterance).';

    document.getElementById('playbtn').onclick = function(){
        text    = document.getElementById('textarea').innerText;
        words   = text.split(' ');
        wordIdx = 0;

        utterance.text = text;
        speechSynthesis.speak(utterance);
    }

    utterance.onboundary = function(event){
        var e = document.getElementById('textarea');
        var it = '';

        for(var i = 0; i < words.length; i++){
            if(i === wordIdx){
                it += '<strong>' + words[i] + '</strong>';
            } else {
                it += words[i];
            }

            it += ' ';
        }

        e.innerHTML = it;
        wordIdx++;
    }
}

最佳答案

您的代码不起作用,但我只是编写了一个可以按您希望的方式工作的示例。打开 fiddle 看看它的工作

var utterance = new SpeechSynthesisUtterance();
var wordIndex = 0;
var global_words = [];
utterance.lang = 'en-UK';
utterance.rate = 1;


document.getElementById('playbtn').onclick = function(){
    var text    = document.getElementById('textarea').value;
    var words   = text.split(" ");
    global_words = words;
    // Draw the text in a div
    drawTextInPanel(words);
    spokenTextArray = words;
    utterance.text = text;
    speechSynthesis.speak(utterance);
};

utterance.onboundary = function(event){
    var e = document.getElementById('textarea');
    var word = getWordAt(e.value,event.charIndex);
    // Show Speaking word : x
    document.getElementById("word").innerHTML = word;
    //Increase index of span to highlight
    console.info(global_words[wordIndex]);

    try{
        document.getElementById("word_span_"+wordIndex).style.color = "blue";
    }catch(e){}

    wordIndex++;
};

utterance.onend = function(){
        document.getElementById("word").innerHTML = "";
    wordIndex = 0;
    document.getElementById("panel").innerHTML = "";
};

// Get the word of a string given the string and the index
function getWordAt(str, pos) {
    // Perform type conversions.
    str = String(str);
    pos = Number(pos) >>> 0;

    // Search for the word's beginning and end.
    var left = str.slice(0, pos + 1).search(/\S+$/),
        right = str.slice(pos).search(/\s/);

    // The last word in the string is a special case.
    if (right < 0) {
        return str.slice(left);
    }
    // Return the word, using the located bounds to extract it from the string.
    return str.slice(left, right + pos);
}

function drawTextInPanel(words_array){
console.log("Dibujado");
        var panel = document.getElementById("panel");
    for(var i = 0;i < words_array.length;i++){
        var html = '<span id="word_span_'+i+'">'+words_array[i]+'</span>&nbsp;';
        panel.innerHTML += html;
    }
}

请玩以下 fiddle :

Highlight spoken word SpeechSynthesis Javascript fiddle

它用蓝色高亮了div中的口语,你可以自定义加粗的样式,但重要的是思路。

注意:请记住,onboundary 事件仅针对 native (本地)语音合成触发。更改 Google 示例(即 Google UK English Male)中指定的语音以获取 google 远程语音,将使您的代码失败,因为 SpeechSynthesis API 似乎只能播放由 google 服务器生成的声音。

关于javascript - 语音合成 API 在说话时突出显示单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38120478/

相关文章:

javascript - 何时使用 WinJS.Navigation.state 与选项

javascript - MongoDB 管理

javascript - 在 PhoneGap 浏览器中存储用户数据

android - 如何在 Google Glass 中安装语音转文本功能?

javascript - 在 chrome android 上使用 speechSynthesis.resume() 恢复暂停的语音不起作用

.net - 使用 SpeechSynthesizer 使用 SpeechAudioFormatInfo 流式传输 TTS

javascript - React Native 保存输入数据

jquery - 如何为响应式标签添加背景

javascript - 显示和隐藏一个 div onclick

javascript - 我的 onclick 附加到在同一函数中被删除的类。为什么它仍然被触发?