将HTML文本同步并突出显示为音频

时间:2012-05-24 18:59:13

标签: html5 html5-audio

如果有必要,我可以更详细地解释,但基本上我需要做的是与音频轨道同步地对CSS文本进行CSS更改 - 即,突出显示与音频回放同步的单词/短语。我还需要通过单击文本来控制音频播放。我有很好的HTML / CSS印章,但我对原始js不那么强,所以我希望有一个jQuery方法。我希望有人能引导我走向最佳方向。

非常感谢,

SVS

1 个答案:

答案 0 :(得分:18)

为了便于使用,我建议将jQuery和Popcorn.js组合在一起,以便将媒体与HTML集成,反之亦然。有关示例,请参阅此jsfiddle post

为了记录,如果jsfiddle永远消失,这是代码:

HTML

<audio id="greeting" src="https://dl.dropboxusercontent.com/u/17154625/greeting.ogg" controls></audio>

<div id="text">
   <span id="w1" class="word" data-start="1.0">Hello</span>,
   and <span id="w2" class="word" data-start="2.0">welcome</span>
   to Stack <span id="w3" class="word" data-start="3.0">Overflow</span>.
   Thank you for asking your question.
</div>​

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();
});​