溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么使用JS開發(fā)簡(jiǎn)單的音樂播放器

發(fā)布時(shí)間:2021-11-06 14:56:08 來源:億速云 閱讀:134 作者:iii 欄目:web開發(fā)

本篇內(nèi)容介紹了“怎么使用JS開發(fā)簡(jiǎn)單的音樂播放器”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

首先,最終效果如圖所示:

怎么使用JS開發(fā)簡(jiǎn)單的音樂播放器

首先,我們來編寫html界面index.html,代碼如下:

<!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>

<!--播放器-->

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

<!--整體結(jié)構(gòu)-->

<div id="boxDiv">

<!--歌詞展示區(qū)域-->

<div id="contentDiv">

<!--歌詞顯示-->

<div id="lrcDiv"></div>

<!--上部陰影-->

<span id="bgTopSpan"></span>

<!--下部陰影-->

<span id="bgBottomSpan"></span>

</div>

<!--控制區(qū)域-->

<div id="controlDiv">

<!--進(jìn)度條-->

<div id="progressDiv"></div>

<!--進(jìn)度條圓點(diǎn)-->

<img id="pointerImg" src="img/audio/progress_pointer@2x.png"/>

<!--播放時(shí)間-->

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

<!--歌曲標(biāo)題-->

<span id="titleSpan"></span>

<!--總時(shí)間-->

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

<!--按鈕區(qū)域-->

<div id="buttonDiv">

<!--上一首按鈕-->

<img id="prevImg" src="img/audio/play_above_song@2x.png"/>

<!--播放暫停按鈕-->

<img id="playOrPauseImg" src="img/audio/play@2x.png"/>

<!--下一首按鈕-->

<img id="nextImg" src="img/audio/play_next_song@2x.png"/>

</div>

</div>

</div>

</body>

</html>

接下來,編寫style.css,代碼如下:

body{

margin: px;

background-color: #ccc;

}

#boxDiv{

width: 375px;

height: 568px;

margin: 10px auto;

position: relative;

}

#contentDiv{

width: 375px;

height: 460px;

background-image: url(../img/audio/play_bg@2x.png);

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: px;

background-image: url(../img/audio/play_top_shadow@2x.png);

}

#bgBottomSpan{

top: 430px;

position: absolute;

background-image: url(../img/audio/play_bottom_shadow@2x.png);

display: block;

width: 375px;

height: 30px;

}

#controlDiv{

width: 375px;

height: 180px;

position: relative;

background-color: white;

}

#progressDiv{

background-color: red;

height: 1.5px;

width: px;

}

#pointerImg{

width: 18px;

height: 18px;

position: absolute;

top: -8.5px;

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;

}

最后,編寫common.js,代碼如下:

$(function(){

// 歌曲列表

var playList = ["紅日", "狼的誘惑", "漂洋過海來看你"];

// 當(dāng)前播放的歌曲序號(hào)

var currentIndex = ;

// 播放器的原生對(duì)象

var audio = $("#song")[];

// 播放時(shí)間數(shù)組

var timeArr = [];

// 歌詞數(shù)組

var lrcArr = [];

// 調(diào)用播放前設(shè)置

setup();

// 播放歌曲

playMusic();

// 播放歌曲

function playMusic(){

// 播放歌曲

audio.play();

// 設(shè)置為暫停的圖片

$("#playOrPauseImg").attr("src", "img/audio/pause@2x.png");

}

// 歌曲播放前設(shè)置

function setup(){

// 設(shè)置播放哪一首歌曲

// img/audio/紅日.mp3

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

// 設(shè)置歌曲的名字

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

// 設(shè)置歌詞

setLrc();

}

// 設(shè)置歌詞

function setLrc(){

// 清空上一首的歌詞

$("#lrcDiv span").remove();

// 清空數(shù)組

timeArr = [];

lrcArr = [];

// 加載歌詞文件

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

if(data){

// 按行切割字符串

var arr = data.split("\n");

// 分割歌詞

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

// 分割時(shí)間和歌詞

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

if (tempArr.length > 1){

// 添加時(shí)間和歌詞到數(shù)組

timeArr.push(tempArr[]);

lrcArr.push(tempArr[1]);

}

}

// 顯示歌詞

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

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

}

}

});

}

// 播放暫停事件

$("#playOrPauseImg").click(function(){

// 如果播放器是暫停狀態(tài)

if(audio.paused){

// 繼續(xù)播放

playMusic();

}else{

// 暫停

audio.pause();

// 變成播放的圖片

$("#playOrPauseImg").attr("src", "img/audio/play@2x.png");

}

});

// 上一首

$("#prevImg").click(function(){

// 如果是第一首,那么跳到最后一首

if(currentIndex == ){

currentIndex = playList.length - 1;

}else{

currentIndex--;

}

// 播放前設(shè)置

setup();

// 播放

playMusic();

});

// 下一首

$("#nextImg").click(function(){

// 如果是最后一首,就跳到第一首

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

currentIndex = ;

}else{

currentIndex++;

}

// 播放前設(shè)置

setup();

// 播放

playMusic();

});

// 監(jiān)聽播放器播放時(shí)間改變事件

audio.addEventListener("timeupdate", function(){

// 當(dāng)前播放時(shí)間

var currentTime = audio.currentTime;

// 總時(shí)間

var totalTime = audio.duration;

// 當(dāng)是數(shù)字的時(shí)候

if(!isNaN(totalTime)){

// 得到播放時(shí)間與總時(shí)長(zhǎng)的比值

var rate = currentTime / totalTime;

// 設(shè)置時(shí)間顯示

// 播放時(shí)間

$("#playTimeSpan").text(getFormatterDate(currentTime));

// 總時(shí)長(zhǎng)

$("#totalTimeSpan").text(getFormatterDate(totalTime));

// 設(shè)置進(jìn)度條

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

// 設(shè)置進(jìn)度圓點(diǎn)

$("#pointerImg").css("left", (375 - 15) * rate - 3 + "px");

// 設(shè)置歌詞的顏色和內(nèi)容的滾動(dòng)

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

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

// 當(dāng)前播放時(shí)間大于等于i行的時(shí)間,并且小于下一行的時(shí)間

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

// 當(dāng)前行歌詞變紅色

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

// 其他行歌詞變白色

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

// 當(dāng)前行歌詞滾動(dòng)

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

}

}

}

}

});

function getTime(timeStr){

// 去掉左邊的[

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

if(arr.length > 1){

// 得到右邊的時(shí)間

var str = arr[1];

// 分割分、秒

var tempArr = str.split(":");

// 分

var m = parseInt(tempArr[]);

// 秒

var s = parseFloat(tempArr[1]);

return m * 60 + s;

}

return ;

}

// 格式化時(shí)間(00:00)

function getFormatterDate(time){

// 分

var m = parseInt(time / 60);

// 秒

var s = parseInt(time % 60);

// 補(bǔ)零

m = m > 9 ? m : "0" + m;

s = s > 9 ? s : "0" + s;

return m + ":" + s;

}

});

圖片和歌曲、歌詞請(qǐng)自行下載,最后,可以運(yùn)行試試了。

“怎么使用JS開發(fā)簡(jiǎn)單的音樂播放器”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

js
AI