怎么用HTML5做一个时代少年团歌曲播放器

发布网友

我来回答

2个回答

懂视网

本篇文章主要介绍了HTML5网页音乐播放器的示例代码,内容挺不错的,现在分享给大家,也给大家做个参考。

本文介绍了HTML5网页音乐播放器的示例代码,分享给大家,具体如下:

1功能介绍

HTML5中推出了音视频标签,可以让我们不借助其他插件就可以直接播放音视频。下面我们就利用H5的audio标签及其相关属性和方法来制作一个简单的音乐播放器。主要包括以下几个功能:

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

2、调整音量和播放进度条

3、根据列表切换当前歌曲

先来看一下最终的完成效果:

这个音乐播放器的结构主要分为三部分:歌曲信息、播放器和播放列表,我们重点介绍一下播放器部分。首先在播放器中放三个audio标签用于播放:

<audio id="music1">浏览器不支持audio标签
<source src="media/Beyond - 光辉岁月.mp3"></source>
</audio>
<audio id="music2">浏览器不支持audio标签
<source src="media/Daniel Powter - Free Loop.mp3"></source>
</audio>
<audio id="music3">浏览器不支持audio标签
<source src="media/周杰伦、费玉清 - 千里之外.mp3"></source>
</audio>

下面的播放列表也对应三个audio标签:

<p id="playList">
 <ul>
 <li id="m0">Beyond-光辉岁月</li>
 <li id="m1">Daniel Powter-Free Loop</li>
 <li id="m2">周杰伦、费玉清-千里之外</li>
 </ul>
</p>

接下来我们就开始逐步实现上面提到的功能吧,先来完成播放和暂停功能,在按下播放按钮时我们要做到进度条随歌曲进度前进,播放时间也逐渐增加,同时播放按钮变成暂停按钮,播放列表的样式也对应改变。

在做功能之前我们要先把三个audio标签获取id后存到一个数组中,供后续使用。

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

2播放和暂停:

我们现在就可以完成播放按钮的功能啦,首先设置一个标志,用来标记音乐的播放状态,再为数组的索引index设置一个默认值:

然后对播放状态进行判断,调用对应的函数,并修改flag的值和列表对应项目样式:

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)";
}
}

上面的代码中调用了多个函数,其中播放和暂停是audio标签自带的方法,而其他的函数则是我们自己定义的。下面我们就来看一下这些函数是怎么实现的,又对应了哪些功能吧。

3进度条和播放时间:

首先是进度条功能,获取歌曲的全部时长,然后再根据当前播放的进度与进度条总长度相乘计算出进度条的位置。

function progressBar(){
var lenth=mList[index].duration;
timer1=setInterval(function(){
 cur=mList[index].currentTime;//获取当前的播放时间
 progress.style.width=""+parseFloat(cur/lenth)*300+"px";
 progressBtn.style.left= 60+parseFloat(cur/lenth)*300+"px";
},10)
}

下面是改变播放时间功能,这里我们设置一个定时函数,每隔一段时间来执行一次以改变播放时间。因为我们获取到的歌曲时长是以秒来计算,所以我们要利用if语句对时长判断来进行换算,将播放时间改为以几分几秒的形式来显示。

function playTimes(){
timer2=setInterval(function(){
 cur=parseInt(mList[index].currentTime);//秒数
 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调整播放进度和音量:

接下来我们再来完成一下通过进度条调整播放进度和调整音量功能。

调整播放进度功能利用了event对象来实现,因为offsetX属性只有IE事件具有,所以推荐使用IE浏览器查看效果。先对进度条添加事件监听,当在进度条上点击鼠标时,获取鼠标的位置,并根据位置除以进度条的总长度来计算当前的播放进度,然后对歌曲进行设置。

//调整播放进度
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";
}
})

下面是调整音量功能,音量的调整我们采用拖动的形式实现,思路也是对音量条的按钮球添加事件监听,然后根据拖动的位置来计算按钮球相对于音量条整体的位置,最后根据计算结果与音量相乘得出当前音量:

volBtn.addEventListener("mousedown",function(event){
var e = event || window.event;
var that =this;
//阻止球的默认拖拽事件
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歌曲切换

最后我们再来实现比较复杂的歌曲切换功能。

先来看用上一首和下一首按钮进行切换,在切换音乐时我们要注意的问题有下面几个:第一,我们要停止当前播放的音乐,转而播放下一首音乐;第二,要清空进度条和播放时间,重新计算;第三,歌曲信息要随之改变,播放器下面的播放列表样式也要变化。在弄清楚上面三点以后我们就可以开始实现功能了。

//上一曲
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();
}

通过播放列表来切换歌曲与通过按钮切换的思路差不多,只是根据对应的列表项来设置当前应该播放哪首歌曲。

上面切换歌曲的函数中都调用了几个方法,下面我们来看看这些方法的用途都是什么吧。

首先是切换歌曲信息:

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);
}
}

再把播放的音乐停止,并且将播放时间恢复。

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";
}

到此,音乐播放器我们就基本完成了,来看一下动图的效果:

热心网友

下载时代首年团歌曲进行添加。
HTML5是构建Web内容的一种语言描述方式。HTML5是互联网的下一代标准,是构建以及呈现互联网内容的一种语言方式.被认为是互联网的核心技术之一。HTML产生于1990年,1997年HTML4成为互联网标准,并广泛应用于互联网应用的开发。
HTML5是Web中核心语言HTML的规范,用户使用任何手段进行网页浏览时看到的内容原本都是HTML格式的,在浏览器中通过一些技术处理将其转换成为了可识别的信息。HTML5在从前HTML4.01的基础上进行了一定的改进,虽然技术人员在开发过程中可能不会将这些新技术投入应用,但是对于该种技术的新特性,网站开发技术人员是必须要有所了解的。
HTML5是HyperTextMarkupLanguage5的缩写,HTML5技术结合了HTML4.01的相关标准并革新,符合现代网络发展要求,在2008年正式发布。HTML5由不同的技术构成,其在互联网中得到了非常广泛的应用,提供更多增强网络应用的标准机。与传统的技术相比,HTML5的语法特征更加明显,并且结合了SVG的内容。这些内容在网页中使用可以更加便捷地处理多媒体内容,而且HTML5中还结合了其他元素,对原有的功能进行调整和修改,进行标准化工作。HTML5在2012年已形成了稳定的版本。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com