這篇文章給大家介紹怎么在HTML5頁面中實(shí)現(xiàn)一個(gè)音樂播放器,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
目前創(chuàng)新互聯(lián)建站已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬主機(jī)、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、觀山湖網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
<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/周杰倫、費(fèi)玉清 - 千里之外.mp3"></source> </audio>
下面的播放列表也對(duì)應(yīng)三個(gè)audio標(biāo)簽:
<div id="playList"> <ul> <li id="m0">Beyond-光輝歲月</li> <li id="m1">Daniel Powter-Free Loop</li> <li id="m2">周杰倫、費(fèi)玉清-千里之外</li> </ul> </div>
接下來我們就開始逐步實(shí)現(xiàn)上面提到的功能吧,先來完成播放和暫停功能,在按下播放按鈕時(shí)我們要做到進(jìn)度條隨歌曲進(jìn)度前進(jìn),播放時(shí)間也逐漸增加,同時(shí)播放按鈕變成暫停按鈕,播放列表的樣式也對(duì)應(yīng)改變。
在做功能之前我們要先把三個(gè)audio標(biāo)簽獲取id后存到一個(gè)數(shù)組中,供后續(xù)使用。
var music1= document.getElementById("music1"); var music2= document.getElementById("music2"); var music3= document.getElementById("music3"); var mList = [music1,music2,music3];
2播放和暫停:
我們現(xiàn)在就可以完成播放按鈕的功能啦,首先設(shè)置一個(gè)標(biāo)志,用來標(biāo)記音樂的播放狀態(tài),再為數(shù)組的索引index設(shè)置一個(gè)默認(rèn)值:
然后對(duì)播放狀態(tài)進(jìn)行判斷,調(diào)用對(duì)應(yīng)的函數(shù),并修改flag的值和列表對(duì)應(yīng)項(xià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)用了多個(gè)函數(shù),其中播放和暫停是audio標(biāo)簽自帶的方法,而其他的函數(shù)則是我們自己定義的。下面我們就來看一下這些函數(shù)是怎么實(shí)現(xiàn)的,又對(duì)應(yīng)了哪些功能吧。
3進(jìn)度條和播放時(shí)間:
首先是進(jìn)度條功能,獲取歌曲的全部時(shí)長,然后再根據(jù)當(dāng)前播放的進(jìn)度與進(jìn)度條總長度相乘計(jì)算出進(jìn)度條的位置。
function progressBar(){ var lenth=mList[index].duration; timer1=setInterval(function(){ cur=mList[index].currentTime;//獲取當(dāng)前的播放時(shí)間 progress.style.width=""+parseFloat(cur/lenth)*300+"px"; progressBtn.style.left= 60+parseFloat(cur/lenth)*300+"px"; },10) }
下面是改變播放時(shí)間功能,這里我們?cè)O(shè)置一個(gè)定時(shí)函數(shù),每隔一段時(shí)間來執(zhí)行一次以改變播放時(shí)間。因?yàn)槲覀儷@取到的歌曲時(shí)長是以秒來計(jì)算,所以我們要利用if語句對(duì)時(shí)長判斷來進(jìn)行換算,將播放時(shí)間改為以幾分幾秒的形式來顯示。
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); }
4調(diào)整播放進(jìn)度和音量:
接下來我們?cè)賮硗瓿梢幌峦ㄟ^進(jìn)度條調(diào)整播放進(jìn)度和調(diào)整音量功能。
調(diào)整播放進(jìn)度功能利用了event對(duì)象來實(shí)現(xiàn),因?yàn)閛ffsetX屬性只有IE事件具有,所以推薦使用IE瀏覽器查看效果。先對(duì)進(jìn)度條添加事件監(jiān)聽,當(dāng)在進(jìn)度條上點(diǎn)擊鼠標(biāo)時(shí),獲取鼠標(biāo)的位置,并根據(jù)位置除以進(jìn)度條的總長度來計(jì)算當(dāng)前的播放進(jìn)度,然后對(duì)歌曲進(jìn)行設(shè)置。
//調(diào)整播放進(jìn)度 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)整我們采用拖動(dòng)的形式實(shí)現(xiàn),思路也是對(duì)音量條的按鈕球添加事件監(jiān)聽,然后根據(jù)拖動(dòng)的位置來計(jì)算按鈕球相對(duì)于音量條整體的位置,最后根據(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; } } })
5歌曲切換
最后我們?cè)賮韺?shí)現(xiàn)比較復(fù)雜的歌曲切換功能。
先來看用上一首和下一首按鈕進(jìn)行切換,在切換音樂時(shí)我們要注意的問題有下面幾個(gè):第一,我們要停止當(dāng)前播放的音樂,轉(zhuǎn)而播放下一首音樂;第二,要清空進(jìn)度條和播放時(shí)間,重新計(jì)算;第三,歌曲信息要隨之改變,播放器下面的播放列表樣式也要變化。在弄清楚上面三點(diǎn)以后我們就可以開始實(shí)現(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)"; } }
下面的代碼是點(diǎn)擊列表切換歌曲。
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ù)對(duì)應(yīng)的列表項(xiàng)來設(shè)置當(dāng)前應(yīng)該播放哪首歌曲。
上面切換歌曲的函數(shù)中都調(diào)用了幾個(gè)方法,下面我們來看看這些方法的用途都是什么吧。
首先是切換歌曲信息:
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 = "周杰倫、費(fèi)玉清"; } }
然后清空兩個(gè)定時(shí)器:
//將進(jìn)度條置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); } }
再把播放的音樂停止,并且將播放時(shí)間恢復(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"; }
關(guān)于怎么在HTML5頁面中實(shí)現(xiàn)一個(gè)音樂播放器就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
當(dāng)前文章:怎么在HTML5頁面中實(shí)現(xiàn)一個(gè)音樂播放器
轉(zhuǎn)載來源:http://aaarwkj.com/article30/gpiipo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、云服務(wù)器、軟件開發(fā)、手機(jī)網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)