您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)JS如何實現(xiàn)可視化音頻效果,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
效果如圖:
背景圖片可以換成自己喜歡的或者不用,線條的顏色粗細(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" name="" value="" id="musicFile"><input type="button" name="startStop" value="暫停" id="startStop"> <p id="tip" ></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();//實例化 $('#musicFile').change(function(){ //當(dāng)選擇歌曲時,判斷當(dāng)前audioContext的狀態(tài),如果在進行中則關(guān)閉音頻環(huán)境, //釋放audioContext的所有資源,并重新實例化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('解碼中,已用時'+count+'秒') },1000) audioContext.decodeAudioData(e.target.result, function(buffer) { clearInterval(timer) $('#tip').text('解碼成功,用時共計:'+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>
關(guān)于“JS如何實現(xiàn)可視化音頻效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。