js实现音乐播放器_第1页
js实现音乐播放器_第2页
js实现音乐播放器_第3页
js实现音乐播放器_第4页
js实现音乐播放器_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第js实现音乐播放器播放控制按钮

就是暂停和播放按钮

判断全局变量isplay,如果是true说明当前正在播放歌曲,点击就会暂停,反之就会播放

//播放控制

document.getElementById('btnPlay').addEventListener('click',function(){

if(isplay){

pausePlay();

}else{

startPlay();

}

});

将当前歌曲的播放时长与总时长在界面上动态改变

//记录歌曲的当前播放时间

varnow=0;

//记录歌曲的总播放时长

vartotal=0;

//当播放器的数据被加载时触发

player.addEventListener('loadeddata',function(){

//获取当前播发器的播放位置以及总播放时长(单位s)

now=player.currentTime;

total=player.duration;

//将歌曲的播放时间显示到控制区域

document.querySelector('.play-current').innerText=fmtTime(now);

document.querySelector('.play-duration').innerText=fmtTime(total);

})

为进度条绑定进度改变事件

原理很简单,通过上面的时间变化求得百分比,设置为进度的百分比就OK了

//为播放器绑定播放进度改变事件

player.addEventListener('timeupdate',updateProgress);

functionupdateProgress(){

//获取当前的播放进度

now=player.currentTime;

//计算进度的百分比

varp=now/total*100+'%';

document.querySelector('.progress-control').style.width=p;

document.querySelector('.play-current').innerText=fmtTime(now);

}

为播放器绑定播放完成事件以及上下首的切换

同意要清除上一首歌的播放状态,改变全局变量currentIndex就可以实现

//为播放器绑定播放完成事件

player.addEventListener('ended',function(){

//清除上一首播放状态

clearstatus();

currentIndex++;

if(currentIndex=musics.length){

currentIndex=0;

}

//重新为播放器设置播放源

player.src=musics[currentIndex].path;

//继续播放

startPlay();

//上一首

document.querySelector('.btn-pre').addEventListener('click',function(){

clearstatus();

currentIndex--;

if(currentIndex0){

currentIndex=musics.length-1;

}

//重新为播放器设置播放源

player.src=musics[currentIndex].path;

//继续播放

startPlay();

//下一首

document.querySelector('.btn-next').addEventListener('click',function(){

clearstatus();

currentIndex++;

currentIndex=currentIndex%musics.length;

//重新为播放器设置播放源

player.src=musics[currentIndex].path;

//继续播放

startPlay();

});

通过进度条控制歌曲播放

对鼠标进行监听,得到鼠标最后的落点,计算出进度条的起始位置与该点占据总长度的比例,然后简单的数学运算,我们知道歌曲总长度就很清楚的得到鼠标落点的歌曲该播放的位置

//改变歌曲的播放进度

(function(box,bar){

//监听鼠标是否按下

varstatus=false;

//鼠标按下事件监听

document.querySelector(box).addEventListener('mousedown',function(e){

player.removeEventListener('timeupdate',updateProgress);

move(e);

status=true;

})

//鼠标抬起事件监听

document.addEventListener('mouseup',function(){

player.currentTime=now;

player.addEventListener('timeupdate',updateProgress);

status=false;

})

//鼠标移动事件监听

document.querySelector(box).addEventListener('mousemove',function(e){

if(status==true){

move(e)

}

})

//鼠标滑动执行

functionmove(e){

vareventLeft=e.offsetX;

varw=window.getComputedStyle(document.querySelector(box)).width;

varw1=window.getComputedStyle(document.querySelector('.play-current')).width;

varw2=window.getComputedStyle(document.querySelector('.play-duration')).width;

w1=parseInt(w1);

w2=parseInt(w2);

document.querySelector(bar).style.width=eventLeft+w1+'px';

now=(eventLeft)/(parseInt(w)-w1-w2)*total;

status=true;

//vareventLeft=e.offsetX;

//document.querySelector(bar).style.width=eventLeft+'px';

//varw=window.getComputedStyle(document.queryS

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论