JavaScript - HTML5 Audio / custom player#39;s seekbar and current time(JavaScript-HTML5音频/自定义播放器的搜索栏和当前时间)
本文介绍了JavaScript-HTML5音频/自定义播放器的搜索栏和当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在开发一个定制的HTML5音频播放器,但我似乎在某些方面遗漏了一些东西:
<input>
范围滑块没有跟在音频之后,并且播放当前时间的<div>
范围不起作用-它正在一个接一个地添加零。
以下是我到目前为止的代码:
HTML
<div class="player">
<audio id="HAE">
<source src="https://upload.wikimedia.org/wikipedia/commons/transcoded/b/b1/Haussperling2008.ogg/Haussperling2008.ogg.mp3" type="audio/mpeg">
</audio>
<div id="playpause">PLAY</div>
<input id="progress" type="range" min="0" max="100" value="0" step="0.1">
<div id="ct">00:00</div>
</div>
JavaScript
// VARIABLES
hae = document.getElementById('HAE');
pp = document.getElementById('playpause');
progress = document.getElementById('progress');
seeking = false;
seekto = hae.duration * (progress.value / 100);
ct = document.getElementById('ct');
time = hae.currentTime * (100 / hae.duration);
mins = Math.floor(hae.currentTime / 60);
secs = Math.floor(hae.currentTime - mins * 60);
// EVENTS
pp.addEventListener('click', togglePlay);
progress.addEventListener('mousedown', function(event) {seeking = true; seek(event);});
progress.addEventListener('mousemove', function(event) {seek(event);});
progress.addEventListener('mouseup', function() {seeking = false;});
hae.addEventListener('timeupdate', function(){ seekTimeUpdate(); });
// TOGGLE PLAY/PAUSE
function togglePlay() {
if (hae.paused) {
hae.play();
pp.innerHTML = "PAUSE";
}
else {
hae.pause();
pp.innerHTML = "PLAY";
}
}
// PROGRESS BAR
function seek(event){
if(seeking){
progress.value = event.clientX - progress.offsetLeft;
hae.currentTime = seekto;
}
}
// MM:SS
function seekTimeUpdate(){
progress.value = time;
if(secs < 10) {secs = "0" + secs;}
if(mins < 10) {mins = "0" + mins;}
ct.innerHTML = mins + ":" + secs;
}
这里是一个正在工作的Fiddle。
有人能帮我解决我的问题吗?
提前谢谢!
推荐答案
secs = "0" + secs;
前缀"0"
到secs
,因此随着seekTimeUpdate
在timeupdate
事件中被调用,此字符串将不断增加。
Date
和toISOString
来避免担心细节,但这样的方法在必要时也应该有效:
const padTime = n => (~~(n) + "").padStart(2, "0");
const fmtTime = s =>
s < 1 ? "00:00" : `${padTime(s / 60)}:${padTime(s % 60)}`
;
数据-lang="js"数据-隐藏="假"数据-控制台="真"数据-巴贝尔="假">
const url = "https://upload.wikimedia.org/wikipedia/commons/transcoded/b/b1/Haussperling2008.ogg/Haussperling2008.ogg.mp3";
const audio = new Audio(url);
const playBtn = document.querySelector("button");
const currTimeEl = document.querySelector(".current-time");
const durationEl = document.querySelector(".duration");
const progressEl = document.querySelector('input[type="range"]');
let mouseDownOnSlider = false;
const fmtTime = s => {
const d = new Date(0);
if (s > 0) {
d.setSeconds(s % 60);
d.setMinutes(s / 60);
}
return d.toISOString().slice(14, 19);
};
audio.addEventListener("loadeddata", () => {
progressEl.value = 0;
currTimeEl.textContent = fmtTime(audio.currentTime);
durationEl.textContent = fmtTime(audio.duration);
});
audio.addEventListener("timeupdate", () => {
if (!mouseDownOnSlider) {
progressEl.value = audio.currentTime / audio.duration * 100;
currTimeEl.textContent = fmtTime(audio.currentTime);
durationEl.textContent = fmtTime(audio.duration);
}
});
audio.addEventListener("ended", () => {
playBtn.textContent = "▶️";
});
playBtn.addEventListener("click", () => {
audio.paused ? audio.play() : audio.pause();
playBtn.textContent = audio.paused ? "▶️" : "⏸️";
});
progressEl.addEventListener("change", () => {
const pct = progressEl.value / 100;
audio.currentTime = (audio.duration || 0) * pct;
});
progressEl.addEventListener("mousedown", () => {
mouseDownOnSlider = true;
});
progressEl.addEventListener("mouseup", () => {
mouseDownOnSlider = false;
});
button {
font-size: 1.5em;
}
<button>▶️</button>
<input type="range" value="0" min="0" max="100" step="1"><span class="current-time">00:00</span>/<span class="duration">00:00</span>
这篇关于JavaScript-HTML5音频/自定义播放器的搜索栏和当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:JavaScript-HTML5音频/自定义播放器的搜索栏和当前时间
基础教程推荐
猜你喜欢
- 我什么时候应该在导入时使用方括号 2022-01-01
- 动态更新多个选择框 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06