- 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.tw/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.tw/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 檔案,並在網頁上自動播放。
廣告