JS實(shí)現(xiàn)可視化音頻效果的實(shí)例代碼
效果如圖:
背景圖片可以換成自己喜歡的或者不用,線條的顏色粗細(xì)也可以自己調(diào)整。

代碼如下(可直接復(fù)制使用):
<html lang="en">
<head>
<meta charset="UTF-8">
<title>可視化音頻</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<style> body {
display: block;
background: url("./8.jpg");
background-position: center;
background-repeat: no-repeat;
background-attachment: fixed;
background-size:100%;
}
</style>
</head>
<body>
<input type="file" style="color:red;" name="" value="" id="musicFile"><input type="button" name="startStop" value="暫停" id="startStop">
<p id="tip" style="color:red;"></p>
<canvas id="canvas"></canvas>
<script>
window.onload = function () {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var canvasCtx = canvas.getContext("2d");
var AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
var audioContext = new AudioContext();//實(shí)例化
$('#musicFile').change(function(){
//當(dāng)選擇歌曲時(shí),判斷當(dāng)前audioContext的狀態(tài),如果在進(jìn)行中則關(guān)閉音頻環(huán)境,
//釋放audioContext的所有資源,并重新實(shí)例化audioContext
if(audioContext.state == 'running'){
audioContext.close();
audioContext = new AudioContext();
}
if (this.files.length == 0) return;
var file = $('#musicFile')[0].files[0];
var fileReader = new FileReader();
fileReader.readAsArrayBuffer(file);
fileReader.onload = function(e) {
var count = 0;
$('#tip').text('開始解碼')
var timer = setInterval(function(){
count++;
$('#tip').text('解碼中,已用時(shí)'+count+'秒')
},1000)
audioContext.decodeAudioData(e.target.result, function(buffer) {
clearInterval(timer)
$('#tip').text('解碼成功,用時(shí)共計(jì):'+count+'秒')
var audioBufferSourceNode = audioContext.createBufferSource();
var analyser = audioContext.createAnalyser();
analyser.fftSize = 256;
audioBufferSourceNode.connect(analyser);
analyser.connect(audioContext.destination);
audioBufferSourceNode.buffer = buffer;
audioBufferSourceNode.start();
var bufferLength = analyser.frequencyBinCount;
var dataArray = new Uint8Array(bufferLength);
//播放暫停音頻
startStop.onclick = function() {
if(audioContext.state === 'running') {
audioContext.suspend().then(function() {
$("#startStop").val('播放');
});
} else if(audioContext.state === 'suspended') {
audioContext.resume().then(function() {
$("#startStop").val('暫停');
});
}
}
var oW = canvas.width;
var oH = canvas.height;
var color1 = canvasCtx.createLinearGradient(oW / 2, oH / 2-10, oW / 2, oH / 2 - 150);
var color2 = canvasCtx.createLinearGradient(oW / 2, oH / 2+10, oW / 2, oH / 2 + 150);
color1.addColorStop(0, '#1E90FF');
color1.addColorStop(.25, '#FF7F50');
color1.addColorStop(.5, '#8A2BE2');
color1.addColorStop(.75, '#4169E1');
color1.addColorStop(1, '#00FFFF');
color2.addColorStop(0, '#1E90FF');
color2.addColorStop(.25, '#FFD700');
color2.addColorStop(.5, '#8A2BE2');
color2.addColorStop(.75, '#4169E1');
color2.addColorStop(1, '#FF0000');
function draw() {
drawVisual = requestAnimationFrame(draw);
var barHeight;
// 自定義獲取數(shù)組里邊數(shù)據(jù)的頻步
canvasCtx.clearRect(0, 0, oW, oH);
for (var i = 0; i < bufferLength; i++) {
barHeight = dataArray[i];
analyser.getByteFrequencyData(dataArray);
// 繪制向上的線條
canvasCtx.fillStyle = color1;
/* context.fillRect(x,y,width,height)
* x,y是坐標(biāo)
* width,height線條的寬高
*/
canvasCtx.fillRect(oW / 2 + (i * 8), oH / 2, 2, -barHeight);
canvasCtx.fillRect(oW / 2 - (i * 8), oH / 2, 2, -barHeight);
// 繪制向下的線條
canvasCtx.fillStyle = color2;
canvasCtx.fillRect(oW / 2 + (i * 8), oH / 2, 2, barHeight);
canvasCtx.fillRect(oW / 2 - (i * 8), oH / 2, 2, barHeight);
}
};
draw();
})
}
})
}
</script>
</body>
</html>
總結(jié)
以上所述是小編給大家介紹的JS實(shí)現(xiàn)可視化音頻效果的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
- JavaScript可視化圖表庫D3.js API中文參考
- 如何實(shí)現(xiàn)json數(shù)據(jù)可視化詳解
- Highcharts+NodeJS搭建數(shù)據(jù)可視化平臺(tái)示例
- JS 音頻可視化插件Wavesurfer.js的使用教程
- JavaScript數(shù)據(jù)可視化:ECharts制作地圖
- 交互式可視化js庫gojs使用介紹及技巧
- vue使用wavesurfer.js解決音頻可視化播放問題
- 配合Swagger使用絕佳的兩款直觀易用JSON可視化工具
- JS前端可視化GraphQL使用詳解
- 相關(guān)JavaScript在覽器中實(shí)現(xiàn)可視化的四種方式
相關(guān)文章
基于Javascript實(shí)現(xiàn)文件實(shí)時(shí)加載進(jìn)度的方法
不知道大家有沒有發(fā)現(xiàn)在現(xiàn)在的移動(dòng)頁面上,有很多情況需要加載大量的資源。但是移動(dòng)端的訪問速度和pc還是有很大的差距,有些時(shí)候需要一些取巧的方式來提升用戶體驗(yàn),而實(shí)時(shí)顯示加載進(jìn)度就是其中一種。這篇文章就給大家分享了Javascript實(shí)現(xiàn)文件實(shí)時(shí)加載進(jìn)度的方法。2016-10-10
僅30行代碼實(shí)現(xiàn)Javascript中的MVC
這篇文章主要介紹了僅30行代碼實(shí)現(xiàn)Javascript中的MVC的方法,MVC的基礎(chǔ)是觀察者模式,這是實(shí)現(xiàn)model和view同步的關(guān)鍵,想要深入了解的朋友可以參考本文2016-02-02
原生javascript移動(dòng)端滑動(dòng)banner效果
這篇文章主要為大家詳細(xì)介紹了原生javascript移動(dòng)端滑動(dòng)banner效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03
用javascript實(shí)現(xiàn)畫圖效果的代碼
用javascript實(shí)現(xiàn)畫圖效果的代碼...2007-07-07
純JavaScript實(shí)現(xiàn)的兼容各瀏覽器的添加和移除事件封裝
這篇文章主要介紹了純JavaScript實(shí)現(xiàn)的兼容各瀏覽器的添加和移除事件封裝,本文直接給出實(shí)現(xiàn)代碼,代碼中帶詳細(xì)注釋,需要的朋友可以參考下2015-03-03
Bootstrap基本組件學(xué)習(xí)筆記之下拉菜單(7)
這篇文章主要為大家詳細(xì)介紹了Bootstrap基本組件學(xué)習(xí)筆記之下拉菜單,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
用javascript實(shí)現(xiàn)jquery的document.ready功能的實(shí)現(xiàn)代碼
實(shí)現(xiàn)jQuery的document.ready功能js代碼2009-11-11
深入理解Canvas模糊問題產(chǎn)生的原因與解決辦法
我們在使用Canvas進(jìn)行繪圖時(shí),經(jīng)常會(huì)出現(xiàn)繪制的文字或者圖片比較模糊,這篇文章我們就來討論一下Canvas模糊問題產(chǎn)生的原因與解決辦法吧2024-04-04

