HTML5網(wǎng)頁音樂播放器

一、功能介紹

HTML5中推出了音視頻標(biāo)簽,可以讓我們不借助其他插件就可以直接播放音視頻。下面我們就利用H5的audio標(biāo)簽及其相關(guān)屬性和方法來制作一個簡單的音樂播放器。主要包括以下幾個功能:

1、播放暫停、上一首和下一首

2、調(diào)整音量和播放進度條

3、根據(jù)列表切換當(dāng)前歌曲

先來看一下最終的完成效果:


image

這個音樂播放器的結(jié)構(gòu)主要分為三部分:歌曲信息、播放器和播放列表,我們重點介紹一下播放器部分。首先在播放器中放三個audio標(biāo)簽用于播放:

<audio id="music1">瀏覽器不支持audio標(biāo)簽
<source  src="media/Beyond - 光輝歲月.mp3"></source>
</audio>
<audio id="music2">瀏覽器不支持audio標(biāo)簽
<source  src="media/Daniel Powter - Free Loop.mp3"></source>
</audio>
<audio id="music3">瀏覽器不支持audio標(biāo)簽
<source  src="media/周杰倫、費玉清 - 千里之外.mp3"></source>
</audio>

下面的播放列表也對應(yīng)三個audio標(biāo)簽:

<div id="playList">
    <ul>
        <li id="m0">Beyond-光輝歲月</li>
        <li id="m1">Daniel Powter-Free Loop</li>
        <li id="m2">周杰倫、費玉清-千里之外</li>
    </ul>
</div>

接下來我們就開始逐步實現(xiàn)上面提到的功能吧,先來完成播放和暫停功能,在按下播放按鈕時我們要做到進度條隨歌曲進度前進,播放時間也逐漸增加,同時播放按鈕變成暫停按鈕,播放列表的樣式也對應(yīng)改變。

在做功能之前我們要先把三個audio標(biāo)簽獲取id后存到一個數(shù)組中,供后續(xù)使用。

var music1= document.getElementById("music1");
var music2= document.getElementById("music2");
var music3= document.getElementById("music3");
var mList = [music1,music2,music3];

二、播放與暫停

我們現(xiàn)在就可以完成播放按鈕的功能啦,首先設(shè)置一個標(biāo)志,用來標(biāo)記音樂的播放狀態(tài),再為數(shù)組的索引index設(shè)置一個默認(rèn)值:

然后對播放狀態(tài)進行判斷,調(diào)用對應(yīng)的函數(shù),并修改flag的值和列表對應(yīng)項目樣式:

function playMusic(){
if(flag&&mList[index].paused){
            mList[index].play();
        document.getElementById("m"+index).style.backgroundColor = "#A71307";
document.getElementById("m"+index).style.color = "white";
progressBar();
        playTimes();
        play.style.backgroundImage = "url(media/pause.png)";
        flag = false;
}else{
        mList[index].pause();
        flag = true;
        play.style.backgroundImage = "url(media/play.png)";
}
}

上面的代碼中調(diào)用了多個函數(shù),其中播放和暫停是audio標(biāo)簽自帶的方法,而其他的函數(shù)則是我們自己定義的。下面我們就來看一下這些函數(shù)是怎么實現(xiàn)的,又對應(yīng)了哪些功能吧。

三、進度條和播放時間

首先是進度條功能,獲取歌曲的全部時長,然后再根據(jù)當(dāng)前播放的進度與進度條總長度相乘計算出進度條的位置。

function progressBar(){
var lenth=mList[index].duration;
timer1=setInterval(function(){
        cur=mList[index].currentTime;//獲取當(dāng)前的播放時間
        progress.style.width=""+parseFloat(cur/lenth)*300+"px";
        progressBtn.style.left= 60+parseFloat(cur/lenth)*300+"px";
},10)
}

下面是改變播放時間功能,這里我們設(shè)置一個定時函數(shù),每隔一段時間來執(zhí)行一次以改變播放時間。因為我們獲取到的歌曲時長是以秒來計算,所以我們要利用if語句對時長判斷來進行換算,將播放時間改為以幾分幾秒的形式來顯示。

function playTimes(){
timer2=setInterval(function(){
        cur=parseInt(mList[index].currentTime);//秒數(shù)
        var minute=parseInt(cur/60);
        if (minute<10) {
            if(cur%60<10){
                playTime.innerHTML="0"+minute+":0"+cur%60+"";
            }else{
                playTime.innerHTML="0"+minute+":"+cur%60+"";
            }
        } else{
            if(cur%60<10){
                playTime.innerText= minute+":0"+cur%60+"";
            }else{
                playTime.innerText= minute+":"+cur%60+"";
            } 
        } 
},1000);
}

四、調(diào)整播放進度和音量

接下來我們再來完成一下通過進度條調(diào)整播放進度和調(diào)整音量功能。

調(diào)整播放進度功能利用了event對象來實現(xiàn),因為offsetX屬性只有IE事件具有,所以推薦使用IE瀏覽器查看效果。先對進度條添加事件監(jiān)聽,當(dāng)在進度條上點擊鼠標(biāo)時,獲取鼠標(biāo)的位置,并根據(jù)位置除以進度條的總長度來計算當(dāng)前的播放進度,然后對歌曲進行設(shè)置。

//調(diào)整播放進度
total.addEventListener("click",function(event){
var e = event || window.event;
document.onmousedown = function(event){
        var e = event || window.event;
        var mousePos1 = e.offsetX;
        var maxValue1 = total.scrollWidth;
        mList[index].currentTime = (mousePos1/300)*mList[index].duration;
        progress.style.width = mousePos1+"px";
        progressBtn.style.left = 60+ mousePos1 +"px";
}
})

下面是調(diào)整音量功能,音量的調(diào)整我們采用拖動的形式實現(xiàn),思路也是對音量條的按鈕球添加事件監(jiān)聽,然后根據(jù)拖動的位置來計算按鈕球相對于音量條整體的位置,最后根據(jù)計算結(jié)果與音量相乘得出當(dāng)前音量:

volBtn.addEventListener("mousedown",function(event){
var e = event || window.event;
var that =this;
//阻止球的默認(rèn)拖拽事件
e.preventDefault();
document.onmousemove = function(event){
var e = event || window.event;
var mousePos2 = e.offsetY;
var maxValue2 = vol.scrollHeight;
if(mousePos2<0){
            mousePos2 = 0;
}
if(mousePos2>maxValue2){
            mousePos2=maxValue2;
}
mList[index].volume = (1-mousePos2/maxValue2);
console.log(mList[index].volume);
volBtn.style.top = (mousePos2)+"px";
volBar.style.height = 60-(mousePos2)+"px";
document.onmouseup = function(event){
            document.onmousemove = null;
            document.onmouseup = null;
}
}
})

五、歌曲切換

最后我們再來實現(xiàn)比較復(fù)雜的歌曲切換功能。

先來看用上一首和下一首按鈕進行切換,在切換音樂時我們要注意的問題有下面幾個:第一,我們要停止當(dāng)前播放的音樂,轉(zhuǎn)而播放下一首音樂;第二,要清空進度條和播放時間,重新計算;第三,歌曲信息要隨之改變,播放器下面的播放列表樣式也要變化。在弄清楚上面三點以后我們就可以開始實現(xiàn)功能了。

//上一曲
function prevM(){
clearInterval(timer1);
clearInterval(timer2);
stopM();
qingkong();
cleanProgress();
--index;
if(index==-1){
        index=mList.length-1;
}
clearListBgc();
document.getElementById("m"+index).style.backgroundColor = "#A71307";
document.getElementById("m"+index).style.color = "white";
changeInfo(index);
mList[index].play();
progressBar();
playTimes();
if (mList[index].paused) {
    play.style.backgroundImage = "url(media/play.png)";
}else{
    play.style.backgroundImage = "url(media/pause.png)";
}
} 
//下一曲
function nextM(){
clearInterval(timer1);
clearInterval(timer2);
stopM();
qingkong();
cleanProgress();
++index;
if(index==mList.length){
    index=0;
}
clearListBgc();
document.getElementById("m"+index).style.backgroundColor = "#A71307";
document.getElementById("m"+index).style.color = "white";
changeInfo(index);
mList[index].play();
progressBar();
playTimes();
if (mList[index].paused) {
    play.style.backgroundImage = "url(media/play.png)";
}else{
    play.style.backgroundImage = "url(media/pause.png)";
}
}

下面的代碼是點擊列表切換歌曲。

m0.onclick = function (){
clearInterval(timer1);
clearInterval(timer2);
qingkong();
flag = false;
stopM();
index = 0;
pauseall();
play.style.backgroundImage = "url(media/pause.png)";
clearListBgc();
document.getElementById("m0").style.backgroundColor = "#A71307";
document.getElementById("m"+index).style.color = "white";
mList[index].play();
cleanProgress();
progressBar();
changeInfo(index);
playTimes();
}
m1.onclick = function (){
clearInterval(timer1);
clearInterval(timer2);
flag = false;
qingkong();
stopM();
index = 1;
pauseall();
clearListBgc();
play.style.backgroundImage = "url(media/pause.png)";
document.getElementById("m1").style.backgroundColor = "#A71307";
document.getElementById("m"+index).style.color = "white";
mList[index].play();
cleanProgress();
changeInfo(index);
progressBar();
playTimes();
}
m2.onclick = function (){
clearInterval(timer1);
clearInterval(timer2);
flag = false;
qingkong();
stopM();
index = 2;
pauseall();
play.style.backgroundImage = "url(media/pause.png)";
clearListBgc();
document.getElementById("m2").style.backgroundColor = "#A71307";
document.getElementById("m"+index).style.color = "white";
mList[index].play();
cleanProgress();
changeInfo(index);
progressBar();
playTimes();
}

通過播放列表來切換歌曲與通過按鈕切換的思路差不多,只是根據(jù)對應(yīng)的列表項來設(shè)置當(dāng)前應(yīng)該播放哪首歌曲。

上面切換歌曲的函數(shù)中都調(diào)用了幾個方法,下面我們來看看這些方法的用途都是什么吧。

首先是切換歌曲信息:

function changeInfo(index){
if (index==0) {
    musicName.innerHTML = "光輝歲月";
    singer.innerHTML = "Beyond";
}
if (index==1) {
    musicName.innerHTML = "Free Loop";
    singer.innerHTML = "Daniel Powter";
}
if (index==2) {
    musicName.innerHTML = "千里之外";
    singer.innerHTML = "周杰倫、費玉清";
}
} 

然后清空兩個定時器:

//將進度條置0
function cleanProgress(timer1){
if(timer1!=undefined){
    clearInterval(timer1);
}
progress.style.width="0";
progressBtn.style.left="60px";
} 
function qingkong(timer2){ 
if(timer2!=undefined){
    clearInterval(timer2);
}
}

再把播放的音樂停止,并且將播放時間恢復(fù)

function stopM(){
if(mList[index].played){
    mList[index].pause();
    mList[index].currentTime=0;
    flag=false;
}
}

最后的最后,改變下面播放列表的樣式:

function clearListBgc(){
document.getElementById("m0").style.backgroundColor = "#E5E5E5";
document.getElementById("m1").style.backgroundColor = "#E5E5E5";
document.getElementById("m2").style.backgroundColor = "#E5E5E5";
document.getElementById("m0").style.color = "black";
document.getElementById("m1").style.color = "black";
document.getElementById("m2").style.color = "black";
}

到此,音樂播放器我們就基本完成了,來看一下動圖的效果:


image

作者:杰瑞教育Allen老師
版權(quán)聲明:本文版權(quán)歸煙臺杰瑞教育科技有限公司和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、通過CocoaPods安裝項目名稱項目信息 AFNetworking網(wǎng)絡(luò)請求組件 FMDB本地數(shù)據(jù)庫組件 SD...
    陽明AI閱讀 16,186評論 3 119
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,161評論 4 61
  • //1.將常見的結(jié)構(gòu)體包裝成oc對象NSValue,然后存到NSArray中CGPoint point1 = CG...
    Jason_Wong閱讀 1,479評論 0 1
  • 坐在店里,打開手機,又在思考這一周的文章已經(jīng)坐著有兩個多小時了,可是還是沒有什么頭緒。各種內(nèi)容在腦袋里反復(fù)的閃現(xiàn),...
    萬康閱讀 220評論 0 0
  • 非常高興群里的小伙伴這次推薦的書《正見》,老實說,特別感恩能遇見這樣一個同頻的伙伴圈,每天一條讀書語音分享大家互相...
    橙小羊的世界閱讀 603評論 0 5

友情鏈接更多精彩內(nèi)容