【发布时间】:2018-09-02 14:32:11
【问题描述】:
我有一个 JavaScript 音频播放器,带有向前/向后跳过 10 第二个按钮。我通过设置音频元素的currentTime 来做到这一点:
function Player(skipTime)
{
this.skipTime = skipTime;
this.waitLoad = false;
// initialise main narration audio
this.narration = new Audio(getFileName(dynamicNarration));
this.narration.preload = "auto";
this.narration.addEventListener('canplaythrough', () => { this.loaded(); });
this.narration.addEventListener('timeupdate', () => { this.seek(); });
this.narration.addEventListener('ended', () => { this.ended(); });
this.narration.addEventListener('waiting', () => { this.audioWaiting(); });
this.narration.addEventListener('playing', () => { this.loaded(); });
}
Player.prototype = {
rew: function rew()
{
if (!this.waitLoad) {
this.skip(-this.skipTime);
}
},
ffw: function ffw()
{
if (!this.waitLoad) {
this.skip(this.skipTime);
}
},
skip: function skip(amount)
{
const curTime = this.narration.currentTime;
const newTime = curTime + amount;
console.log(`Changing currentTime (${curTime}) to ${newTime}`);
this.narration.currentTime = newTime;
console.log(`Result: currentTime = ${this.narration.currentTime}`);
},
loaded: function loaded()
{
if (this.waitLoad) {
this.waitLoad = false;
playButton.removeClass('loading');
}
},
audioWaiting: function audioWaiting()
{
if (!this.waitLoad) {
this.waitLoad = true;
playButton.addClass('loading');
}
},
}
(我在这里包括了一些我正在附加的事件侦听器,因为以前我调试过一个类似的问题,因为事件侦听器中的冲突。不过,这次彻底调试了事件侦听器,我不 认为这是问题的根源。)
虽然这一切在我的本地副本上都可以正常工作,但当我测试在线版本时,我得到以下结果:
-
Chrome:将播放位置重置为
0。最后的控制台行读取结果:currentTime = 0。 -
Safari: 根本不改变播放位置。最后的
console.log行给出的currentTime值等于newTime(即使播放位置实际上没有改变)。 -
Firefox: 向前跳过作品;向后跳过会中断音频几秒钟,然后它会从播放头所在的几秒钟前再次开始播放。在这两种情况下,最后的 console.log 行给出的
currentTime值等于newTime
这个问题一定与音频的加载方式有关。我尝试添加另一个控制台日志行来显示buffered 的开始值和结束值。
在 Chrome 中,它会在当前播放位置后上升到 2 秒。在 Safari 中,它上升到 ~170 秒,而在 Firefox 中,它似乎缓冲了完整的音频长度。
但是,在每种情况下,buffered 对象的开头都是 0。
有人知道可能出了什么问题吗?
【问题讨论】:
-
Igid,如果您对我下面的回答感到满意,请在我的回答左侧将其标记为已接受或给我写反馈。
-
@lgid 您能否提供您正在使用的文件的样本?它是什么类型的?我已经使用托管在某处的 mp3 文件尝试了您的代码,并且它按预期工作。
标签: javascript event-handling html5-audio dom-events audio-player