Javascript player

by lemon kazi

HTML

<audio id="player" src="http://www.alexkatz.me/codepen/music/interlude.mp3"></audio>
<div id="audioplayer">
 <button id="pButton" class="pause"></button>
 <div id="timeline">    
	<div id="playhead"></div>
 </div>
</div>

CSS

#audioplayer{
    position: relative;
	width: 480px;
	height: 60px;
	margin: 50px auto auto auto;
    border: solid;
}

#pButton{
    height:60px; 
    width: 60px;
    border: none;
    float:left;
    outline:none;
}

#pButton.pause {
    background: url('http://www.alexkatz.me/codepen/images/play.png') no-repeat;
    background-size: 50%;
    background-position: center;
}

#pButton.play {
    background: url('http://www.alexkatz.me/codepen/images/pause.png') no-repeat;
    background-size: 50%;
    background-position: center;
}

#timeline{
	width: 418px;
	height: 20px;
	margin-top: 20px;
	float: left;
	border-radius: 15px;
	background: rgba(0,0,0,.3);
    position: relative;
}
#playhead{
	width: 18px;
	height: 18px;
	border-radius: 50%;
	margin-top: 1px;
	background: rgba(0, 0, 0,1);
    position: absolute;
    left: 0px;
}

JavaScript

var audio = document.getElementById("player");
var btn = document.getElementById("pButton");
var playhead = document.getElementById("playhead");
btn.addEventListener("click", function(){
   if (audio.paused) {
   	   audio.play();
       btn.classList.remove("pause");
       btn.classList.add("play");
   } else  {
   	   audio.pause();
       btn.classList.remove("play");
       btn.classList.add("pause");
   }
});

audio.addEventListener("timeupdate", function(){
   var duration = this.duration;
   var currentTime = this.currentTime;
   var percentage = (currentTime / duration) * 100;
   
   playhead.style.left = percentage * 4 + 'px';
});