I'm working on a custom HTML5 audio player, but it seems i missed something at some point:
The <input>
range slider is not following audio, and the <div>
span for the playback's current time is not working - it's adding zeros next to each other.
Here is my code so far:
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;
}
Here is a working Fiddle.
Can someone help me to fix my problem?
Thanks in advance!