Good programmer front-end share using JS to develop simple music player, recently, we teach students to use JavaScript, today with you to develop a simple music player. First, the final effect looks like this:



First, let’s write the HTML interface index.html, the code is as follows:

<! DOCTYPE html>

<html>

<head>

<meta charset=”utf-8″ />

<title></title>

<link rel=”stylesheet” type=”text/css” href=”css/style.css”/>

< script SRC = “js/jquery – 2.1.0. Js” type = “text/javascript” charset = “utf-8” > < / script >

<script src=”js/common.js” type=”text/javascript” charset=”utf-8″></script>

</head>

<body>

<! — player –>

<audio id=”song” autoplay=”autoplay”></audio>

<! — Overall structure –>

<div id=”boxDiv”>

<! — Lyrics display area –>

<div id=”contentDiv”>

<! — Lyrics display –>

<div id=”lrcDiv”></div>

<! — Upper shadow –>

<span id=”bgTopSpan”></span>

<! — Lower shadow –>

<span id=”bgBottomSpan”></span>

</div>

<! — Control area –>

<div id=”controlDiv”>

<! — Progress bar –>

<div id=”progressDiv”></div>

<! — Progress bar dot –>

<img id=”pointerImg” src=”img/audio/[email protected]”/>

<! — Play time –>

<span id=”playTimeSpan”>00:00</span>

<! — Song Title –>

<span id=”titleSpan”></span>

<! — Total time –>

<span id=”totalTimeSpan”>00:00</span>

<! — Button area –>

<div id=”buttonDiv”>

<! — Previous button –>

<img id=”prevImg” src=”img/audio/[email protected]”/>

<! Play pause button –>

<img id=”playOrPauseImg” src=”img/audio/[email protected]”/>

<! — Next button –>

<img id=”nextImg” src=”img/audio/[email protected]”/>

</div>

</div>

</div>

</body>

</html>


Next, write style.css as follows:

body{

margin: 0px;

background-color: #ccc;

}


#boxDiv{

width: 375px;

height: 568px;

margin: 10px auto;

position: relative;

}


#contentDiv{

width: 375px;

height: 460px;

background-image: url(.. /img/audio/[email protected]);

overflow: hidden;

}


#lrcDiv{

margin-top: 260px;

}


#lrcDiv span{

display: block;

line-height: 40px;

text-align: center;

color: white;

font-size: 20px;

}


#bgTopSpan{

position: absolute;

display: block;

width: 375px;

height: 30px;

top: 0px;

background-image: url(.. /img/audio/[email protected]);

}


#bgBottomSpan{

top: 430px;

position: absolute;

background-image: url(.. /img/audio/[email protected]);

display: block;

width: 375px;

height: 30px;

}


#controlDiv{

width: 375px;

height: 180px;

position: relative;

background-color: white;

}


#progressDiv{

background-color: red;

Height: 1.5 px;

width: 0px;

}


#pointerImg{

width: 18px;

height: 18px;

position: absolute;

Top: 8.5 px;

left: -3px;

}


#playTimeSpan{

display: block;

position: absolute;

font-size: 14px;

width: 35px;

top: 5px;

left: 5px;

}


#totalTimeSpan{

display: block;

position: absolute;

font-size: 14px;

width: 35px;

top: 5px;

left: 335px;

}


#titleSpan{

display: block;

position: absolute;

height: 30px;

width: 295px;

font-size: 20px;

text-align: center;

left: 40px;

top: 5px;

}


#buttonDiv{

margin-top: 40px;

position: relative;

}


#prevImg{

width: 40px;

height: 40px;

position: absolute;

top: 20px;

left: 60px;

}


#playOrPauseImg{

width: 70px;

height: 70px;

position: absolute;

top: 5px;

left: 152px;

}


#nextImg{

width: 40px;

height: 40px;

position: absolute;

top: 20px;

left: 275px;

}


Finally, write common.js with the following code:

$(function(){

// List of songs

Var playList = [” red sun “, “Wolf “,” cross the sea to see you “];

// The sequence number of the current song

var currentIndex = 0;

// The native object of the player

var audio = $(“#song”)[0];

// Play time array

var timeArr = [];

// Array of lyrics

var lrcArr = [];


// Invoke pre-play Settings

setup();

// Play the song

playMusic();


// Play the song

function playMusic(){

// Play the song

audio.play();

// Set the image to pause

$(“#playOrPauseImg”).attr(“src”, “img/audio/[email protected]”);

}


// Set before the song plays

function setup(){

// Set which song to play

// img/audio/ redsun.mp3

audio.src = “img/audio/” + playList[currentIndex] + “.mp3”;

// Set the name of the song

$(“#titleSpan”).text(playList[currentIndex]);

// Set the lyrics

setLrc();

}


// Set the lyrics

function setLrc(){

// Delete the lyrics of the previous song

$(“#lrcDiv span”).remove();

// Empty the array

timeArr = [];

lrcArr = [];

// Load the lyrics file

$.get(“img/audio/” + playList[currentIndex] + “.lrc”, {}, function(data){

if(data){

// Cut the string by line

var arr = data.split(“\n”);

// Split lyrics

for (var i = 0; i < arr.length; i++) {

// Split time and lyrics

var tempArr = arr[i].split(“]”);

if (tempArr.length > 1){

// Add the time and lyrics to the array

timeArr.push(tempArr[0]);

lrcArr.push(tempArr[1]);

}

}

// Display the lyrics

for (var i = 0; i < lrcArr.length; i++) {

$(“#lrcDiv”).append(“<span>”+lrcArr[i]+”</span>”);

}

}

});

}


// Play the pause event

$(“#playOrPauseImg”).click(function(){

// If the player is paused

if(audio.paused){

// Continue playing

playMusic();

}else{

/ / pause

audio.pause();

// Change the image to play

$(“#playOrPauseImg”).attr(“src”, “img/audio/[email protected]”);

}

});


/ / a

$(“#prevImg”).click(function(){

// If it is the first song, then skip to the last one

if(currentIndex == 0){

currentIndex = playList.length – 1;

}else{

currentIndex–;

}

// Set before playback

setup();

/ / play

playMusic();

});

/ / a

$(“#nextImg”).click(function(){

// If it is the last song, skip to the first one

if(currentIndex == playList.length – 1){

currentIndex = 0;

}else{

currentIndex++;

}

// Set before playback

setup();

/ / play

playMusic();

});


// Listen for player playback time changes

audio.addEventListener(“timeupdate”, function(){

// Current playback time

var currentTime = audio.currentTime;

/ / total time

var totalTime = audio.duration;

// When it is a number

if(! isNaN(totalTime)){

// Get the ratio of playback time to total elapsed time

var rate = currentTime / totalTime;

// Set the time display

// Play time

$(“#playTimeSpan”).text(getFormatterDate(currentTime));

/ / total duration

$(“#totalTimeSpan”).text(getFormatterDate(totalTime));

// Set the progress bar

$(“#progressDiv”).css(“width”, rate * 375 + “px”);

// Set the progress dot

$(“#pointerImg”).css(“left”, (375 – 15) * rate – 3 + “px”);

// Set the lyrics color and content scroll

for (var i = 0; i < timeArr.length – 1; i++) {

if(! isNaN(getTime(timeArr[i]))){

// The current playback time is greater than or equal to line I and less than the next line

if (currentTime >= getTime(timeArr[i]) && currentTime < getTime(timeArr[i+1])){

// The current line of lyrics changes to red

$(“#lrcDiv span:eq(“+i+”)”).css(“color”, “#FF0000”);

// Other lines of lyrics go white

$(“#lrcDiv span:not(:eq(“+i+”))”).css(“color”, “#FFFFFF”);

// Current line lyrics scroll

$(“#lrcDiv”).stop(false, true).animate({“margin-top”: 260 – 40 * i + “px”}, 1000);

}

}

}

}

});


function getTime(timeStr){

// remove the left [

var arr = timeStr.split(“[“);

if(arr.length > 1){

// Get the time on the right

var str = arr[1];

// Split minutes and seconds

var tempArr = str.split(“:”);

/ / points

var m = parseInt(tempArr[0]);

/ / SEC.

var s = parseFloat(tempArr[1]);

return m * 60 + s;

}

return 0;

}


// Format time (00:00)

function getFormatterDate(time){

/ / points

var m = parseInt(time / 60);

/ / SEC.

var s = parseInt(time % 60);

/ / zero padding

m = m > 9 ? m : “0” + m;

s = s > 9 ? s : “0” + s;

return m + “:” + s;

}

});


Pictures and songs, lyrics please download, finally, you can run a try.