因此,我使用了this个问题答案作为检测脚本中的Audio
对象是否正在加载的基础。最初,该函数运行良好,但是我注意到,如果在播放Audio
对象时更改其源,缓冲似乎总是设置为true
。
这就是我所拥有的( JavaScript ):
var _Player = {
audio: null, buffering: false, lastPlayTime: 0,
checkBuffering: function(){
var buffer = setInterval(function(){
var currentTime = this.audio.currentTime, offset = (100 - 20) / 1000;
if(!this.buffering && currentTime < (this.lastPlayTime + offset) && !this.audio.paused){
this.buffering = true;
console.log("loading");
}
if(this.buffering && currentTime > (this.lastPlayTime + offset) && !this.audio.paused){
this.buffering = false;
console.log("not loading");
}
this.lastPlayTime = currentTime;
}, 100);
},
load: function(url){
if(this.audio){
this.audio.src = url;
} else {
this.audio = new Audio(url);
}
this.checkBuffering();
}
};
和一个简短的 HTML 示例:
<!-- when I click on this one, the output to console is correctly shown as 'true', then 'false' for the remainder of the output !-->
<button onclick="_Player.load(some_url);">Test 1</button>
<!-- but when I click this one afterwards, the output is basically swapped ('false' briefly, then 'true' for the remainder) !-->
<button onclick="_Player.load(some_other_url);">Test 2</button>
不知道为什么会这样,但是我觉得它与尝试检查Audio
源是否一次加载(?)有关。感谢所有帮助,
干杯。