gpt4 book ai didi

javascript - 显示音频的缓冲百分比

转载 作者:行者123 更新时间:2023-11-30 09:28:42 24 4
gpt4 key购买 nike

为什么我无法使用 Javascript 显示音频的缓冲百分比?

var audio = document.getElementById("aone1");
var percentages = document.getElementById("aone1l");

function loop() {
var buffered = audio.buffered;
var loaded;
var played;

if (buffered.length) {
loaded = 100 * buffered.end(0) / audio.duration;
played = 100 * audio.currentTime / audio.duration;
percentages[0].innerHTML = loaded.toFixed(2);
percentages[1].innerHTML = played.toFixed(2);
}

setTimeout(loop, 50);
}

loop();
<audio id="aone1" controls="controls">
<source src="http://jainvidhya.epizy.com/Audio/Part1/ऑडियो नंबर 1 नमस्कार महामंत्र.mp4">
</audio>
<p>Loaded: <span id="aone1l"></span>%</p>
<p>Played: <span id="aone1l"></span>%</p>

另外,为什么音频需要很长时间才能加载?

最佳答案

使用唯一的id或在此处使用类,这将为您提供一个数组,然后设置innerHTML:

var audio = document.getElementById("aone1");
var percentages = document.getElementsByClassName("aone1l")

function loop() {
var buffered = audio.buffered;
var loaded;
var played;

if (buffered.length) {
loaded = 100 * buffered.end(0) / audio.duration;
played = 100 * audio.currentTime / audio.duration;
percentages[0].innerHTML = loaded.toFixed(2);
percentages[1].innerHTML = played.toFixed(2);
}

setTimeout(loop, 50);
}

loop();
<audio id="aone1" controls="controls">
<source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3">
</audio>
<p>Loaded: <span class="aone1l"></span>%</p>
<p>Played: <span class="aone1l"></span>%</p>

关于javascript - 显示音频的缓冲百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47727943/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com