html5 显示多个cue,javascript - HTML5 Audio track oncuechange speed too slow - Stack Overflow

博客作者遇到了音频字幕同步的问题,发现oncuechange事件的触发有时存在延迟和不一致性。他们尝试通过定时器检查活动cue来改善,但效果不明显。寻求增加事件检查频率或优化事件触发时机的方法来提高同步准确性。
摘要由CSDN通过智能技术生成

I have a page that embeds a mp3 audio file along with associated .vtt file containing the subtitles which I loop through and load in to divs on the page.

I have a simple fs function to change the formatting of the relevant subtitle as the audio plays - think of it is a karaoke player, highlighting each line as it needs to be sung:

trackData.oncuechange = function(e) {

var cue = this.activeCues[0];

if(cue){

updateSubs(cue); // Loops cues, lowlights old cues, highlights current cue

}

}

I have noticed occasional and inconsistent delays on the formatting updates which is not desirable for my purpose. In posting audio player timings to to the console trying to figure out what might be causing it I get the following example:

Subtitle1 > Cue Time: 13.000 | Event fired @: 13.213424 | Diff: +0.213424 | Noticable

Subtitle2 > Cue Time: 17.109 | Event fired @: 17.209143 | Diff: +0.100143 | Okay

Subtitle3 > Cue Time: 27.004 | Event fired @: 27.215233 | Diff: +0.211233 | Noticable

Subtitle4 > Cue Time: 30.477 | Event fired @: 30.479385 | Diff: +0.002385 | Great

Subtitle5 > Cue Time: 34.151 | Event fired @: 34.217562 | Diff: +0.066562 | Good

Subtitle6 > Cue Time: 39.748 | Event fired @: 39.966155 | Diff: +0.218155 | Noticable

Subtitle7 > Cue Time: 41.601 | Event fired @: 41.714627 | Diff: +0.113627 | Okay

Hunting around online I have found a couple of references to oncuechange being checked only a few times per second - If this is the case then presumably the longer delays I am seeing are a case of bad timing in conjunction with a system clock and explains the inconsistency.

While I don't need absolutely perfect timings for the event being actioned, if I could get the event being checked more frequently (say every 0.1/s) to reduce the unpredictability then that would be great!

So to the question: Is there a way to A. Increase the frequency of event checks? Or B. Do this differently so that the event timings are more accurate?

Thanks in advance for any advice or suggestions.

Edited: 01/10/2018 - Additional info and adding html5 / jQuery tags

Inspired by this SO thread I tried a page timer that checked for the active cue ID changing and posting the results into the console:

var cueTick = window.setInterval(function() { cueTest() }, 1);

var currentCue = '';

var trackData = null;

var media = document.getElementById('media');

function cueTest() {

if(tD == null) {

trackData = document.getElementById('lyrics').track;

} else {

if(!media.paused) {

var cue = trackData.activeCues[0];

if(cue) {

if(cue.id != currentCue) {

console.log('Cue change: ' + cue.id + ' @' + media.currentTime);

}

currentCue = cue.id;

}

}

}

}

In this test a majority of the new test logs occurred immediately after the oncuechange events. Occasionally it would beat the oncuechange but only by a fraction and not enough to make a difference (see last subtitle timing). I initially has the cueTick delay set to 100, then dropped it to 50, 10 and finally 1 without any noticeable difference in the results:

Subtitle1 > Cue Time: 13.000 | Event fired @: 13.210600 | CueTick @: +13.212663

Subtitle2 > Cue Time: 17.109 | Event fired @: 17.203449 | CueTick @: +17.207203

Subtitle3 > Cue Time: 27.004 | Event fired @: 27.211385 | CueTick @: +27.213652

Subtitle4 > Cue Time: 30.477 | Event fired @: 30.706449 | CueTick @: +30.708527

Subtitle5 > Cue Time: 34.151 | Event fired @: 34.211961 | CueTick @: +34.214185

Subtitle6 > Cue Time: 39.748 | Event fired @: 39.956704 | CueTick @: +39.958703

Subtitle7 > Cue Time: 41.601 | Event fired @: 41.712700 | CueTick @: +41.714778

Subtitle8 > Cue Time: 43.944 | Event fired @: 43.963283 | CueTick @: +43.962736

So still no progress - or at least not enough to consider implementing this different method. Any other suggestions?

Thanks in advance

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值