MP3 Player Using Javascript
Submitted by jaredgwapo on Thursday, January 21, 2016 - 16:53.
Language
In this tutorial, I will teach you how to make a mp3 player using javascript and html. just download the code to use it in your projects or systems.
Thats it, you have successfully created a mp3 player. For more questions, informations, suggestion just comment below or email me at [email protected]
Instructions
Creating our index.php page
- <html>
- <head>
- <link href="css/style.css" rel="stylesheet" type="text/css">
- </head>
- <body>
- <div id="container">
- <div id="mp3_player">
- <canvas id="analyser_render"></canvas>
- </div>
- <input id="seekslider" type="range" min="0" max="500" value="0" step="1">
- <div id="timebox">
- </div>
- <input id="volumeslider" type="range" min="0" max="100" value="100" step="1">
- </div>
- </div>
- </body>
- </html>
Creating our style.css page.
- /* CSS Document */
- body{
- background:#f5f5f5;
- }
- #container{
- width:730px;
- margin:50px auto;
- padding:5px;
- background:#000;
- }
- #playpausebtn{
- background:url(../images/pause.jpg) no-repeat;
- border:none;
- width:30px;
- height:30px;
- cursor:pointer;
- outline:none;
- }
- #mutebtn{
- background:url(../images/speaker1.jpg) no-repeat;
- border:none;
- width:30px;
- height:30px;
- cursor:pointer;
- outline:none;
- }
- #timebox{
- display:inline-block;
- width:80px;
- background:#000;
- border-bottom:1px solid #333;
- text-align:center;
- color:#090;
- font-family:Verdana, Geneva, sans-serif;
- font-size:11px;
- }
- input{
- outline:none;
- }
- input#seekslider{
- width:500px;
- }
- input#volumeslider{
- width:70px;
- }
- input[type='range']{
- -webkit-appearance:none !important;
- margin:0px;
- padding:0px;
- background:#000;
- height:13px;
- border-bottom:1px solid #333;
- }
- input[type='range']::-ms-fill-lower{
- background:#000;
- }
- input[type='range']::-ms-fill-upper{
- background:#000;
- }
- input[type='range']::-moz-range-track{
- border:none;
- background:#000;
- }
- input[type='range']::-webkit-slider-thumb{
- -webkit-appearance:none !important;
- background:radial-gradient(#FFF, #333);
- height:11px;
- width:11px;
- border-radius:100%;
- cursor:pointer;
- }
- input[type='range']::-moz-range-thumb{
- background:radial-gradient(#FFF, #333);
- height:11px;
- width:11px;
- border-radius:100%;
- cursor:pointer;
- }
- input[type='range']::-ms-thumb{
- -webkit-appearance:none !important;
- background:radial-gradient(#FFF, #333);
- height:11px;
- width:11px;
- border-radius:100%;
- cursor:pointer;
- }
- h1{
- font-family:Verdana, Geneva, sans-serif;
- font-size:12px;
- color:#FFF;
- padding:5px;
- }
- #mp3_player{ width:700px; height:110px; background:#000; padding:5px; margin:10px auto; }
- #mp3_player > canvas{ width:700px; height:100px; background:#000; float:left; }
Creating our audio_player.js page
- // JavaScript Document
- var dir = "mp3/";
- var playlist = ["sample","sample2","sample3"];
- var playlist_index = 0;
- var ext = ".mp3"
- var audio = new Audio();
- audio.src = dir+playlist[0]+ext;
- audio.loop = false;
- audio.autoplay = false;
- audio.play();
- var canvas, ctx, source, context, analyser, fbc_array, bars, bar_x, bar_width, bar_height;
- window.addEventListener("load", initMp3Player, false);
- function initMp3Player(){
- context = new webkitAudioContext();
- analyser = context.createAnalyser();
- canvas = document.getElementById('analyser_render');
- ctx = canvas.getContext('2d');
- source = context.createMediaElementSource(audio);
- source.connect(analyser);
- analyser.connect(context.destination);
- frameLooper();
- }
- function frameLooper(){
- window.webkitRequestAnimationFrame(frameLooper);
- fbc_array = new Uint8Array(analyser.frequencyBinCount);
- analyser.getByteFrequencyData(fbc_array);
- ctx.clearRect(0, 0, canvas.width, canvas.height);
- ctx.fillStyle = '#FFF';
- bars = 100;
- for (var i = 0; i < bars; i++) {
- bar_x = i * 3;
- bar_width = 2;
- bar_height = -(fbc_array[i] / 2);
- ctx.fillRect(bar_x, canvas.height, bar_width, bar_height);
- }
- }
- function initAudioPlayer(){
- var playbtn, mutebtn, seekslider, volumeslider, seeking=false, seekto, current_time, duration_time, playlist_status;
- playbtn = document.getElementById("playpausebtn");
- mutebtn = document.getElementById("mutebtn");
- seekslider = document.getElementById("seekslider");
- volumeslider = document.getElementById("volumeslider");
- current_time = document.getElementById("current_time");
- duration_time = document.getElementById("duration_time");
- playlist_status = document.getElementById("playlist_status");
- playlist_status.innerHTML = "Track "+(playlist_index+1)+" - "+ playlist[playlist_index]+ext;
- playbtn.addEventListener("click",playPause);
- mutebtn.addEventListener("click",mute);
- seekslider.addEventListener("mousedown", function(event){ seeking=true; seek(event); });
- seekslider.addEventListener("mousemove", function(event){ seek(event); });
- seekslider.addEventListener("mouseup",function(){ seeking=false; });
- volumeslider.addEventListener("mousemove", setvolume);
- audio.addEventListener("timeupdate", function(){ seektimeupdate(); });
- audio.addEventListener("ended", function(){ switchTrack(); });
- function switchTrack(){
- if(playlist_index == (playlist.length - 1)){
- playlist_index = 0;
- }else{
- playlist_index++;
- }
- playlist_status.innerHTML = "Track "+(playlist_index+1)+" - "+ playlist[playlist_index]+ext;
- audio.src = dir+playlist[playlist_index]+ext;
- audio.play();
- }
- function playPause(){
- if(audio.paused){
- audio.play();
- playbtn.style.background = "url(images/pause.jpg) no-repeat";
- }else{
- audio.pause();
- playbtn.style.background = "url(images/play.jpg) no-repeat";
- }
- }
- function mute(){
- if(audio.muted){
- audio.muted = false;
- mutebtn.style.background = "url(images/speaker1.jpg) no-repeat";
- }else{
- audio.muted = true;
- mutebtn.style.background = "url(images/mute1.jpg) no-repeat";
- }
- }
- function seek(event){
- if(seeking){
- seekslider.value = event.clientX - seekslider.offsetLeft;
- seekto = audio.duration * (seekslider.value / 500);
- audio.currentTime = seekto;
- }
- }
- function setvolume(){
- audio.volume = volumeslider.value / 100;
- }
- function seektimeupdate(){
- var new_time = audio.currentTime * (500 / audio.duration);
- seekslider.value = new_time;
- var current_mins = Math.floor(audio.currentTime / 60);
- var current_secs = Math.floor(audio.currentTime - current_mins * 60);
- var duration_mins = Math.floor(audio.duration / 60);
- var duration_secs = Math.floor(audio.duration - duration_mins * 60);
- if(current_secs < 10){ current_secs = "0"+current_secs; }
- if(duration_secs < 10){ duration_secs = "0"+duration_secs; }
- if(current_mins < 10){ current_mins = "0"+current_mins; }
- if(duration_mins < 10){ duration_mins = "0"+duration_mins; }
- current_time.innerHTML = current_mins+":"+current_secs;
- duration_time.innerHTML = duration_mins+":"+duration_secs;
- }
- }
- window.addEventListener("load",initAudioPlayer);
Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.
Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.
FOR YOUR OWN SAFETY, PLEASE:
1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
Add new comment
- 1433 views