synchronize and highlight HTML text to audio

后端 未结 1 1116
星月不相逢
星月不相逢 2020-12-28 10:30

If necessary I can explain in more detail but essentially what I need to do is effect CSS changes to HTML text in sync with an audio track - i.e., highlighting words/phrases

1条回答
  •  甜味超标
    2020-12-28 11:11

    For ease of use, I recommend a combination of jQuery and Popcorn.js for anything where you want to integrate media with HTML, and visa versa. See this jsfiddle post for an example.

    For the record, should jsfiddle ever disappear, here's the code:

    HTML

    
    
    
    Hello, and welcome to Stack Overflow. Thank you for asking your question.

    CSS

    .word {
       color: red;
    }
    .word:hover, .word.selected {
        color: blue;
        cursor: pointer;
    }​
    

    JS

    var pop = Popcorn("#greeting");
    
    var wordTimes = {
        "w1": { start: 1, end: 1.5 },
        "w2": { start: 1.9, end: 2.5 },
        "w3": { start: 3, end: 4 }
    };
    
    $.each(wordTimes, function(id, time) {
         pop.footnote({
            start: time.start,
            end: time.end,
            text: '',
            target: id,
            effect: "applyclass",
            applyclass: "selected"
        });
    });
    
    pop.play();
    
    $('.word').click(function() {
        var audio = $('#greeting');
        audio[0].currentTime = parseFloat($(this).data('start'), 10);
        audio[0].play();
    });​
    

    0 讨论(0)
提交回复
热议问题