JS实现可视化音频效果的实例代码

前端之家收集整理的这篇文章主要介绍了JS实现可视化音频效果的实例代码前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

效果如图:

背景图片可以换成自己喜欢的或者不用,线条的颜色粗细也可以自己调整。

JS实现可视化音频效果的实例代码


代码如下(可直接复制使用):

  1. <html lang="en">
  2. <head>
  3. <Meta charset="UTF-8">
  4. <title>可视化音频</title>
  5. <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
  6. <style> body {
  7. display: block;
  8. background: url("./8.jpg");
  9. background-position: center;
  10. background-repeat: no-repeat;
  11. background-attachment: fixed;
  12. background-size:100%;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <input type="file" style="color:red;" name="" value="" id="musicFile"><input type="button" name="startStop" value="暂停" id="startStop">
  18. <p id="tip" style="color:red;"></p>
  19. <canvas id="canvas"></canvas>
  20. <script>
  21. window.onload = function () {
  22. canvas.width = window.innerWidth;
  23. canvas.height = window.innerHeight;
  24. var canvasCtx = canvas.getContext("2d");
  25.  
  26. var AudioContext = window.AudioContext || window.webkitAudioContext || window.mozAudioContext;
  27. var audioContext = new AudioContext();//实例化
  28.  
  29. $('#musicFile').change(function(){
  30. //当选择歌曲时,判断当前audioContext的状态,如果在进行中则关闭音频环境,
  31. //释放audioContext的所有资源,并重新实例化audioContext
  32. if(audioContext.state == 'running'){
  33. audioContext.close();
  34. audioContext = new AudioContext();
  35. }
  36. if (this.files.length == 0) return;
  37. var file = $('#musicFile')[0].files[0];
  38. var fileReader = new FileReader();
  39. fileReader.readAsArrayBuffer(file);
  40. fileReader.onload = function(e) {
  41. var count = 0;
  42. $('#tip').text('开始解码')
  43. var timer = setInterval(function(){
  44. count++;
  45. $('#tip').text('解码中,已用时'+count+'秒')
  46. },1000)
  47. audioContext.decodeAudioData(e.target.result,function(buffer) {
  48. clearInterval(timer)
  49. $('#tip').text('解码成功,用时共计:'+count+'秒')
  50. var audioBufferSourceNode = audioContext.createBufferSource();
  51. var analyser = audioContext.createAnalyser();
  52. analyser.fftSize = 256;
  53. audioBufferSourceNode.connect(analyser);
  54. analyser.connect(audioContext.destination);
  55. audioBufferSourceNode.buffer = buffer;
  56. audioBufferSourceNode.start();
  57. var bufferLength = analyser.frequencyBinCount;
  58. var dataArray = new Uint8Array(bufferLength);
  59.  
  60. //播放暂停音频
  61. startStop.onclick = function() {
  62. if(audioContext.state === 'running') {
  63. audioContext.suspend().then(function() {
  64. $("#startStop").val('播放');
  65. });
  66. } else if(audioContext.state === 'suspended') {
  67. audioContext.resume().then(function() {
  68. $("#startStop").val('暂停');
  69. });
  70. }
  71. }
  72.  
  73. var oW = canvas.width;
  74. var oH = canvas.height;
  75. var color1 = canvasCtx.createLinearGradient(oW / 2,oH / 2-10,oW / 2,oH / 2 - 150);
  76. var color2 = canvasCtx.createLinearGradient(oW / 2,oH / 2+10,oH / 2 + 150);
  77. color1.addColorStop(0,'#1E90FF');
  78. color1.addColorStop(.25,'#FF7F50');
  79. color1.addColorStop(.5,'#8A2BE2');
  80. color1.addColorStop(.75,'#4169E1');
  81. color1.addColorStop(1,'#00FFFF');
  82.  
  83. color2.addColorStop(0,'#1E90FF');
  84. color2.addColorStop(.25,'#FFD700');
  85. color2.addColorStop(.5,'#8A2BE2');
  86. color2.addColorStop(.75,'#4169E1');
  87. color2.addColorStop(1,'#FF0000');
  88. function draw() {
  89. drawVisual = requestAnimationFrame(draw);
  90. var barHeight;
  91. // 自定义获取数组里边数据的频步
  92. canvasCtx.clearRect(0,oW,oH);
  93. for (var i = 0; i < bufferLength; i++) {
  94. barHeight = dataArray[i];
  95. analyser.getByteFrequencyData(dataArray);
  96. // 绘制向上的线条
  97. canvasCtx.fillStyle = color1;
  98. /* context.fillRect(x,y,width,height)
  99. * x,y是坐标
  100. * width,height线条的宽高
  101. */
  102. canvasCtx.fillRect(oW / 2 + (i * 8),oH / 2,2,-barHeight);
  103. canvasCtx.fillRect(oW / 2 - (i * 8),-barHeight);
  104. // 绘制向下的线条
  105. canvasCtx.fillStyle = color2;
  106. canvasCtx.fillRect(oW / 2 + (i * 8),barHeight);
  107. canvasCtx.fillRect(oW / 2 - (i * 8),barHeight);
  108. }
  109. };
  110. draw();
  111. })
  112. }
  113. })
  114. }
  115. </script>
  116. </body>
  117. </html>

总结

以上所述是小编给大家介绍的JS实现可视化音频效果的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

猜你在找的JavaScript相关文章