- HTML 教程
- HTML - 首页
- HTML - 路线图
- HTML - 简介
- HTML - 历史与演变
- HTML - 编辑器
- HTML - 基本标签
- HTML - 元素
- HTML - 属性
- HTML - 标题
- HTML - 段落
- HTML - 字体
- HTML - 块
- HTML - 样式表
- HTML - 格式化
- HTML - 引用
- HTML - 注释
- HTML - 颜色
- HTML - 图片
- HTML - 图片地图
- HTML - 内嵌框架 (Iframe)
- HTML - 短语元素
- HTML - 元标签
- HTML - 类
- HTML - ID
- HTML - 背景
- HTML 表格
- HTML - 表格
- HTML - 表头和标题
- HTML - 表格样式
- HTML - 表格 Colgroup
- HTML - 嵌套表格
- HTML 列表
- HTML - 列表
- HTML - 无序列表
- HTML - 有序列表
- HTML - 定义列表
- HTML 链接
- HTML - 文本链接
- HTML - 图片链接
- HTML - 邮箱链接
- HTML 颜色名称和值
- HTML - 颜色名称
- HTML - RGB
- HTML - HEX
- HTML - HSL
- HTML 表单
- HTML - 表单
- HTML - 表单属性
- HTML - 表单控件
- HTML - 输入属性
- HTML 多媒体
- HTML - 视频元素
- HTML - 音频元素
- HTML - 嵌入多媒体
- HTML 头部
- HTML - 头元素
- HTML - 添加 Favicon
- HTML - Javascript
- HTML 布局
- HTML - 布局
- HTML - 布局元素
- HTML - 使用 CSS 进行布局
- HTML - 响应式设计
- HTML - 符号
- HTML - 表情符号
- HTML - 样式指南
- HTML 图形
- HTML - SVG
- HTML - Canvas
- HTML API
- HTML - Geolocation API
- HTML - 拖放 API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web 存储
- HTML - 服务器发送事件
- HTML 其他
- HTML - 文档对象模型 (DOM)
- HTML - MathML
- HTML - 微数据
- HTML - IndexedDB
- HTML - Web 消息传递
- HTML - Web CORS
- HTML - Web RTC
- HTML 演示
- HTML - 音频播放器
- HTML - 视频播放器
- HTML - 网页幻灯片
- HTML 工具
- HTML - Velocity Draw
- HTML - 二维码
- HTML - Modernizer
- HTML - 验证
- HTML - 颜色选择器
- HTML 参考
- HTML - 速查表
- HTML - 标签参考
- HTML - 属性参考
- HTML - 事件参考
- HTML - 字体参考
- HTML - ASCII 码
- ASCII 码表查询
- HTML - 颜色名称
- HTML - 实体
- MIME 媒体类型
- HTML - URL 编码
- 语言 ISO 代码
- HTML - 字符编码
- HTML - 已弃用的标签
- HTML 资源
- HTML - 快速指南
- HTML - 有用资源
- HTML - 颜色代码生成器
- HTML - 在线编辑器
HTML - 音频播放器
带可视化器的本地 HTML 音频播放器
HTML 功能,包括无需 Flash 的原生音频和视频支持。以下代码基于 HTML、CSS 和 JavaScript。您可以将本地 MP3 文件拖放到容器中。
示例
让我们来看下面的例子,我们将创建一个本地音频可视化器。
HTML 文件<!DOCTYPE html> <html> <style> #instructions { width: 100%; text-align: center; top: 50%; margin-top: -100px; color: #DE3163; } #container { position: absolute; width: 100%; height: 100%; background: #D5F5E3; } #canvas-container { width: 600px; height: 600px; margin: auto; position: relative; top: 50%; margin-top: -263px; margin-right: -61px; } #canvas-copy { opacity: 0.05; -webkit-transform: scaleY(-1); margin-top: -6px; } </style> <body> <div id="container"> <div id="canvas-container"> <canvas width=600 height=300 id="canvas"></canvas> <canvas width=600 height=300 id="canvas-copy"></canvas> </div> <div id="instructions"> <a href="https://tutorialspoint.com/index.htm" align="center"> Tutorials Point</a> <h2 style="font-family:verdana"> Drag Your Local MP3 Files </h2> </div> <div id="button"></div> </div> <script src="js.html"></script> </body> </html>
现在,我们将创建一个与上面 HTML 文件中提到的名称相同的 javascript 文件。
js.html<script> (function() { var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; window.requestAnimationFrame = requestAnimationFrame; })(); window.onload = function() { var element = document.getElementById('container') dropAndLoad(element, init, "ArrayBuffer") } function dropAndLoad(dropElement, callback, readFormat) { var readFormat = readFormat || "DataUrl" dropElement.addEventListener('dragover', function(e) { e.stopPropagation() e.preventDefault() e.dataTransfer.dropEffect = 'copy' }, false) dropElement.addEventListener('drop', function(e) { e.stopPropagation() e.preventDefault() loadFile(e.dataTransfer.files[0]) }, false) function loadFile(files) { var file = files var reader = new FileReader() reader.onload = function(e) { callback(e.target.result) } reader['readAs' + readFormat](file) } } function init(arrayBuffer) { document.getElementById('instructions').innerHTML = 'Audio Loading' window.audioCtx = new AudioContext() window.analyser = audioCtx.createAnalyser() if (window.source) source.noteOff(0) audioCtx.decodeAudioData(arrayBuffer, function(buffer) { window.source = audioCtx.createBufferSource() source.buffer = buffer source.connect(analyser) analyser.connect(audioCtx.destination) source.start(0) var viz = new simpleViz() new visualizer(viz['update'], analyser) document.getElementById('instructions').innerHTML = '' }) } function visualizer(visualization, analyser) { var self = this this.visualization = visualization var last = Date.now() var loop = function() { var dt = Date.now() - last var byteFreq = new Uint8Array(analyser.frequencyBinCount) analyser.getByteFrequencyData(byteFreq) last = Date.now() self.visualization(byteFreq, dt) requestAnimationFrame(loop) } requestAnimationFrame(loop) } function simpleViz(canvas) { var self = this this.canvas = document.getElementById('canvas') this.ctx = this.canvas.getContext("2d") this.copyCtx = document.getElementById('canvas-copy').getContext("2d") this.ctx.fillStyle = '#fff' this.barWidth = 10 this.barGap = 4 this.bars = Math.floor(this.canvas.width / (this.barWidth + this.barGap)) this.update = function(byteFreq) { self.ctx.clearRect(0, 0, self.canvas.width, self.canvas.height) var step = Math.floor(byteFreq.length / self.bars) for (var i = 0; i < self.bars; i++) { var barHeight = byteFreq[i * step] self.ctx.fillRect(i * (self.barWidth + self.barGap), self.canvas.height - barHeight, self.barWidth, barHeight) self.copyCtx.clearRect(0, 0, self.canvas.width, self.canvas.height) self.copyCtx.drawImage(self.canvas, 0, 0) } } } </script>
让我们结合这两个文件并观察我们将获得的输出。
<!DOCTYPE html> <html> <style> #instructions { width: 100%; text-align: center; top: 50%; margin-top: -100px; color: #DE3163; } #container { position: absolute; width: 100%; height: 100%; background: #D5F5E3; } #canvas-container { width: 600px; height: 600px; margin: auto; position: relative; top: 50%; margin-top: -263px; margin-right: -61px; } #canvas-copy { opacity: 0.05; -webkit-transform: scaleY(-1); margin-top: -6px; } </style> <body> <div id="container"> <div id="canvas-container"> <canvas width=600 height=300 id="canvas"></canvas> <canvas width=600 height=300 id="canvas-copy"></canvas> </div> <div id="instructions"> <a href="https://tutorialspoint.com/index.htm" align="center"> Tutorials Point</a> <h2 style="font-family:verdana"> Drag Your Local MP3 Files </h2> </div> <div id="button"></div> </div> <script> (function() { var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame; window.requestAnimationFrame = requestAnimationFrame; })(); window.onload = function() { var element = document.getElementById('container') dropAndLoad(element, init, "ArrayBuffer") } function dropAndLoad(dropElement, callback, readFormat) { var readFormat = readFormat || "DataUrl" dropElement.addEventListener('dragover', function(e) { e.stopPropagation() e.preventDefault() e.dataTransfer.dropEffect = 'copy' }, false) dropElement.addEventListener('drop', function(e) { e.stopPropagation() e.preventDefault() loadFile(e.dataTransfer.files[0]) }, false) function loadFile(files) { var file = files var reader = new FileReader() reader.onload = function(e) { callback(e.target.result) } reader['readAs' + readFormat](file) } } function init(arrayBuffer) { document.getElementById('instructions').innerHTML = 'Audio Loading' window.audioCtx = new AudioContext() window.analyser = audioCtx.createAnalyser() if (window.source) source.noteOff(0) audioCtx.decodeAudioData(arrayBuffer, function(buffer) { window.source = audioCtx.createBufferSource() source.buffer = buffer source.connect(analyser) analyser.connect(audioCtx.destination) source.start(0) var viz = new simpleViz() new visualizer(viz['update'], analyser) document.getElementById('instructions').innerHTML = '' }) } function visualizer(visualization, analyser) { var self = this this.visualization = visualization var last = Date.now() var loop = function() { var dt = Date.now() - last var byteFreq = new Uint8Array(analyser.frequencyBinCount) analyser.getByteFrequencyData(byteFreq) last = Date.now() self.visualization(byteFreq, dt) requestAnimationFrame(loop) } requestAnimationFrame(loop) } function simpleViz(canvas) { var self = this this.canvas = document.getElementById('canvas') this.ctx = this.canvas.getContext("2d") this.copyCtx = document.getElementById('canvas-copy').getContext("2d") this.ctx.fillStyle = '#fff' this.barWidth = 10 this.barGap = 4 this.bars = Math.floor(this.canvas.width / (this.barWidth + this.barGap)) this.update = function(byteFreq) { self.ctx.clearRect(0, 0, self.canvas.width, self.canvas.height) var step = Math.floor(byteFreq.length / self.bars) for (var i = 0; i < self.bars; i++) { var barHeight = byteFreq[i * step] self.ctx.fillRect(i * (self.barWidth + self.barGap), self.canvas.height - barHeight, self.barWidth, barHeight) self.copyCtx.clearRect(0, 0, self.canvas.width, self.canvas.height) self.copyCtx.drawImage(self.canvas, 0, 0) } } } </script> </body> </html>
运行以上代码后,将生成一个输出,其中包含应用了 CSS 的文本,指示拖放本地 MP3 文件以播放音乐。
带播放列表的本地音频播放器
考虑以下示例,我们允许用户上传多个本地 MP3 文件作为播放列表。
<!DOCTYPE html> <html> <body style="background-color:#ABEBC6;"> <audio controls id="y" autoplay></audio> <br> <br> <br> <input type="file" id="x" multiple> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script> var x = document.getElementById("x"), y = document.getElementById("y"); function next(n) { var a = URL.createObjectURL(z[n]); y.setAttribute('src', a); y.play(); } var _next = 0, z, len; x.addEventListener('Change', function() { z = x.z; len = z.length; if (len) { next(_next); } }); y.addEventListener("Completed", function() { _next += 1; next(_next); console.log(len, _next); if ((len - 1) == _next) { _next = -1; } }); </script> </body> </html>
运行以上代码后,将弹出输出窗口,允许用户上传多个 mp3 文件,并在网页上自动播放。
广告