js+audio实现音乐播放器


Posted in Javascript onSeptember 13, 2020

本文实例为大家分享了js+audio实现音乐播放器的具体代码,供大家参考,具体内容如下

<!DOCTYPE html>
<html>

 <head>
 <meta charset="UTF-8">
 <title>音乐播放器</title>
 <link rel="shortcut icon" type="image/x-icon" href="img/an.ico" />
 <link rel="stylesheet" type="text/css" href="css/music_Play.css" />
 </head>

 <body>
 <div class="music_bg">
 <div class="music_cont">
 <audio id="audio1" src=""></audio>
 <div class="music_ctrl">
  <div class="music_btn">
  <div class="btn prev">
  <img id="prev" src="img/prev.png" />
  </div>
  <div class="btn play">
  <img id="play" src="img/pause.png" />
  </div>
  <div class="btn next">
  <img id="next" src="img/next.png" />
  </div>
  </div>
  <div class="music_name" id="music_name"></div>
 </div>
 <div class="music_line">
  <div class="line1" id="line1"></div>
  <div class="line2" id="line2"></div>
 </div>
 </div>
 </div>
 </body>
 <script src="js/audio_play.js" type="text/javascript" charset="utf-8"></script>
</html>
* {
 margin: 0;
 padding: 0;
 -moz-user-select: none;
 -webkit-user-select: none;
 -ms-user-select: none;
}


body {
 overflow-x: hidden;
}

.music_bg {
 width: 100%;
 height: 666px;
 position: absolute;
 background-image: url(../img/bj.jpg);
 background-position: center;
 background-size: cover;
 background-repeat: no-repeat;
}

.music_cont {
 width: 300px;
 height: 300px;
 position: absolute;
 top: 50%;
 left: 50%;
 margin: -150px 0 0 -150px;
 background-color: #000;
 border-radius: 10px;
 box-shadow: #000000 5px 5px 30px 5px
}

.music_line {
 width: 300px;
 height: 20px;
 overflow: hidden;
 position: absolute;
 top: 30%;
}

.line1 {
 width: 0%;
 height: 60%;
 float: left;
 margin-top: 1%;
 margin-right: -2px;
 background-color: rgba(255, 255, 255, 0.9);
}

.line2 {
 background-image: url(../img/point.png);
 height: 100%;
 background-repeat: no-repeat;
 width: 10px;
 background-position: center;
 float: left;
 cursor: pointer;
 margin-left: -4px;
 margin-right: -4px;
}

.music_ctrl {
 width: 300px;
 height: 200px;
 position: absolute;
 bottom: 0;
 background-color: #8c3232;
}

.music_btn {
 width: 300px;
 height: 100px;
 position: relative;
}

.btn {
 width: 33.33%;
 float: left;
 height: 40px;
 margin-top: 50px;
}

.prev img {
 float: right;
 margin: 10px 0px;
 cursor: pointer;
}

.play img {
 margin: 2px 35px;
 cursor: pointer;
}

.next img {
 float: left;
 margin: 10px 0px;
 cursor: pointer;
}

.music_name {
 width: 300px;
 height: 100px;
 position: relative;
 text-align: center;
 line-height: 100px;
 color: #fff;
 font-size: 18px;
}
// 定义索引和定时器
var index = 0,
 timer = null;
// 获取到要操作的对象
var prev = document.getElementById("prev");
var play = document.getElementById("play");
var next = document.getElementById("next");
var audio1 = document.getElementById("audio1");
var music_name = document.getElementById("music_name");
var line1 = document.getElementById("line1");
var line2 = document.getElementById("line2");
// 定义数组存音频相关资料
var music_src = ["1.mp3", "2.mp3", "3.mp3", "4.mp3"];
var music_title = ["白举纲-绅士(live)", "魔鬼中的天使", "下个路口见", "大鱼"];
// 进行初始化音频
audio1.src = "audio/" + music_src[index];
music_name.innerText = music_title[index];
// 按钮是点击事件
play.onclick = function() {
 move1(); // 播放或暂停
};
prev.onclick = function() {
 prev1(); // 上一曲,播放
 move1();
}
next.onclick = function() {
 next1(); // 下一曲,播放
 move1();
 }

 // 下一曲的函数
var next1 = function() { // 索引+1,初始化改变后的音乐播放界面
 if (index == music_src.length - 1) {
  index = 0;
 } else {
  index++;
 }
 audio1.src = "audio/" + music_src[index];
 music_name.innerText = music_title[index];
 }
 // 上一曲的函数
var prev1 = function() { // 索引-1,初始化改变后的音乐播放界面
 if (index == 0) {
  index = music_src.length - 1;
 } else {
  index--;
 }
 audio1.src = "audio/" + music_src[index];
 music_name.innerText = music_title[index];
 }
 // 暂停与播放的函数
var move1 = function() {
 // 判断现在是不是在播放
 if (audio1.paused) { // 没有,播放音乐,改变按钮样式,改变进度条
  audio1.play();
  play.src = "img/play.png";
  timer = setInterval(function() { // 每500毫秒执行一次
  var drtTime = audio1.duration; // 得到音频总时间(如果不放在定时器中会出现下一曲,暂停播放,进度条来回跳动)
  var curTime = audio1.currentTime; // 获取音频当前播放时间
  line1.style.width = (curTime / drtTime) * 100 + "%"; // 计算出进度条的长度
  if (drtTime==curTime) {
   next.onclick();
  }
  console.log(drtTime,curTime);
  }, 500);
 } else { // 播放,关闭音乐,关闭按钮图标
  audio1.pause();
  play.src = "img/pause.png";
  clearInterval(timer);
 }
 }
 // 拖动进度条改变播放进度
var flag = false; // 标记
var mx = null; // 距离
line2.onmousedown = function(event) {
 // 改变状态
 flag = true;
 // 按下鼠标获取距离
 mx = event.pageX - line2.offsetLeft;
 clearInterval(timer);
}
document.body.onmousemove = function(event) {
 // 当状态为true时可以获取
 if (flag) {
  // 滑块的位置=当前鼠标位置-距离
  var x1 = event.pageX - mx;
  // 进度条当前长度=滑块位置-进度条的开始坐标值
  var x2 = x1 - line1.offsetLeft;
  // 用进度条当前长度/进度条总长度得到一个小数
  var x3 = x2 / 295;
  // 取到小数点后3位
  var x4 = x3.toFixed(3);
  if (x4 >= 0 && x4 < 1) {
  // 当百分比在0--1之间时才改变进度条长度
  line1.style.width = x4 * 100 + "%";
  }else if (x4 == 1) {
  next.onclick();
  }
 }
 }
 // 放开鼠标时,状态改变,当前播放时间改变,启动定时器继续工作
document.body.onmouseup = function(event) {
 flag = false; // 状态改变
 var x5 = parseInt(line1.style.width) / 100; // 得到当前进度条的百分比
 var drtTime = audio1.duration; // 得到音频总时间
 audio1.currentTime = (drtTime * x5).toFixed(0); // 改变当前播放时间
 timer = setInterval(function() { // 定时器重启成功
 var curTime = audio1.currentTime;
 line1.style.width = (curTime / drtTime) * 100 + "%";
 }, 500);
}

相关图片

js+audio实现音乐播放器

js+audio实现音乐播放器
js+audio实现音乐播放器
js+audio实现音乐播放器
js+audio实现音乐播放器
js+audio实现音乐播放器

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
javascript开发技术大全-第1章javascript概述
Jul 03 Javascript
JS实现窗口加载时模拟鼠标移动的方法
Jun 03 Javascript
js实现图片点击左右轮播
Jul 08 Javascript
Uploadify上传文件方法
Mar 16 Javascript
浅谈js中字符和数组一些基本算法题
Aug 15 Javascript
Extjs gridpanel 中的checkbox(复选框)根据某行的条件不能选中的解决方法
Feb 17 Javascript
JS表单数据验证的正则表达式(常用)
Feb 18 Javascript
原生javascript实现读写CSS样式的方法详解
Feb 20 Javascript
浅谈js-FCC算法Friendly Date Ranges(详解)
Apr 10 Javascript
详解vue跨组件通信的几种方法
Jun 15 Javascript
vue中,在本地缓存中读写数据的方法
Sep 21 Javascript
支付宝小程序实现省市区三级联动
Jun 21 Javascript
js+canvas实现刮刮奖功能
Sep 13 #Javascript
js+css3实现简单时钟特效
Sep 13 #Javascript
jquery实现简易验证插件封装
Sep 13 #jQuery
利用H5api实现时钟的绘制(javascript)
Sep 13 #Javascript
浅谈javascript事件环微任务和宏任务队列原理
Sep 12 #Javascript
返回上一个url并刷新界面的js代码
Sep 12 #Javascript
Vue和React有哪些区别
Sep 12 #Javascript
You might like
PHP删除数组中特定元素的两种方法
2013/07/02 PHP
使用gd库实现php服务端图片裁剪和生成缩略图功能分享
2013/12/25 PHP
phpMyAdmin安装并配置允许空密码登录
2015/07/04 PHP
基于jQueryUI和Corethink实现百度的搜索提示功能
2016/11/09 PHP
关于php几种字符串连接的效率比较(详解)
2017/02/22 PHP
JavaScript 学习笔记(十六) js事件
2010/02/01 Javascript
JQuery 1.3.2以上版本中出现pareseerror错误的解决方法
2011/01/11 Javascript
javascript中的注释使用与注意事项小结
2011/09/20 Javascript
浅谈Unicode与JavaScript的发展史
2015/01/19 Javascript
jQuery实现新消息闪烁标题提示的方法
2015/03/11 Javascript
jquery淡入淡出效果简单实例
2016/01/14 Javascript
element ui table(表格)实现点击一行展开功能
2018/12/04 Javascript
Vue实现点击显示不同图片的效果
2019/08/10 Javascript
node.js使用 http-proxy 创建代理服务器操作示例
2020/02/10 Javascript
[40:29]2018DOTA2亚洲邀请赛 4.7总决赛 LGD vs Mineski 第一场
2018/04/10 DOTA
零基础写python爬虫之爬虫编写全记录
2014/11/06 Python
常见的在Python中实现单例模式的三种方法
2015/04/08 Python
Python itertools模块详解
2015/05/09 Python
将Python代码打包为jar软件的简单方法
2015/08/04 Python
Android应用开发中Action bar编写的入门教程
2016/02/26 Python
浅谈python类属性的访问、设置和删除方法
2016/07/25 Python
浅谈python对象数据的读写权限
2016/09/12 Python
Python+tkinter使用80行代码实现一个计算器实例
2018/01/16 Python
Python输出\u编码将其转换成中文的实例
2018/12/15 Python
Python二叉树的遍历操作示例【前序遍历,中序遍历,后序遍历,层序遍历】
2018/12/24 Python
python读取几个G的csv文件方法
2019/01/07 Python
Python-接口开发入门解析
2019/08/01 Python
浅谈Python 递归算法指归
2019/08/22 Python
python GUI库图形界面开发之PyQt5信号与槽多窗口数据传递详细使用方法与实例
2020/03/08 Python
基于virtualenv创建python虚拟环境过程图解
2020/03/30 Python
opencv 图像滤波(均值,方框,高斯,中值)
2020/07/08 Python
世界最大域名注册商:GoDaddy
2016/07/24 全球购物
AJAX的全称是什么
2012/11/06 面试题
中国梦口号
2014/06/13 职场文书
商场租赁意向书
2014/07/30 职场文书
三严三实对照检查材料范文
2014/09/23 职场文书