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('開(kāi)始解碼')
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í)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
- JavaScript可視化圖表庫(kù)D3.js API中文參考
- 如何實(shí)現(xiàn)json數(shù)據(jù)可視化詳解
- Highcharts+NodeJS搭建數(shù)據(jù)可視化平臺(tái)示例
- JS 音頻可視化插件Wavesurfer.js的使用教程
- JavaScript數(shù)據(jù)可視化:ECharts制作地圖
- 交互式可視化js庫(kù)gojs使用介紹及技巧
- vue使用wavesurfer.js解決音頻可視化播放問(wèn)題
- 配合Swagger使用絕佳的兩款直觀易用JSON可視化工具
- JS前端可視化GraphQL使用詳解
- 相關(guān)JavaScript在覽器中實(shí)現(xiàn)可視化的四種方式
相關(guān)文章
egg.js的基本使用和調(diào)用數(shù)據(jù)庫(kù)的方法示例
這篇文章主要介紹了egg.js的基本使用和調(diào)用數(shù)據(jù)庫(kù)的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
JS基于設(shè)計(jì)模式中的單例模式(Singleton)實(shí)現(xiàn)封裝對(duì)數(shù)據(jù)增刪改查功能
這篇文章主要介紹了JS基于設(shè)計(jì)模式中的單例模式(Singleton)實(shí)現(xiàn)封裝對(duì)數(shù)據(jù)增刪改查功能.結(jié)合實(shí)例形式分析了javascript基于單例模式結(jié)合ajax針對(duì)數(shù)據(jù)庫(kù)進(jìn)行增刪改查的相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
基于JavaScript實(shí)現(xiàn)本地圖片預(yù)覽
這篇文章主要為大家詳細(xì)介紹了基于JavaScript實(shí)現(xiàn)本地圖片預(yù)覽的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
細(xì)說(shuō)webpack6 Babel的使用詳解
這篇文章主要介紹了細(xì)說(shuō)webpack6 Babel的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
利用jsPDF實(shí)現(xiàn)將圖片轉(zhuǎn)為pdf
這篇文章主要為大家詳細(xì)介紹了如何利用jsPDF實(shí)現(xiàn)將圖片轉(zhuǎn)為pdf的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下2023-08-08
javascript實(shí)現(xiàn)隨機(jī)讀取數(shù)組的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)隨機(jī)讀取數(shù)組的方法,涉及javascript隨機(jī)數(shù)及針對(duì)數(shù)組操作的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
JavaScript實(shí)現(xiàn)跑馬燈抽獎(jiǎng)活動(dòng)實(shí)例代碼解析與優(yōu)化(一)
這篇文章主要介紹了JavaScript實(shí)現(xiàn)跑馬燈抽獎(jiǎng)活動(dòng)實(shí)例代碼解析與優(yōu)化(一)的相關(guān)資料,需要的朋友可以參考下2016-02-02
JavaScript 實(shí)現(xiàn)下雪特效的示例代碼
這篇文章主要介紹了JavaScript 實(shí)現(xiàn)下雪特效的示例代碼,幫助大家利用JavaScript制作特效,感興趣的朋友可以了解下2020-09-09

