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

所属分类: 网络编程 / JavaScript 阅读数: 1135
收藏 0 赞 0 分享

效果如图:

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

在这里插入图片描述

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

<html lang="en">
 <head>
 <meta charset="UTF-8">
 <title>可视化音频</title>
 <script src="https://cdn.zhanzhang360.cn/imgupload/000429/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();//实例化
   
   $('#musicFile').change(function(){
  //当选择歌曲时,判断当前audioContext的状态,如果在进行中则关闭音频环境,
  //释放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;
       // 自定义获取数组里边数据的频步
       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是坐标
     * 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>

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

更多精彩内容其他人还在看

js实现图片上传预览原理分析

这篇文章主要为大家详细介绍了js实现图片上传预览的原理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Angular限制input框输入金额(是小数的话只保留两位小数点)

最近做项目遇到这样的需求输入框要求输入金额,只能输入数字,可以是小数,必须保留小数点后两位。下面分为两部分代码给大家介绍实现代码,需要的的朋友参考下吧
收藏 0 赞 0 分享

详解vue-cli + webpack 多页面实例配置优化方法

本篇文章主要介绍了详解vue-cli + webpack 多页面实例配置优化方法,具有一定的参考价值,有兴趣的可以了解一下
收藏 0 赞 0 分享

详解React-Native解决键盘遮挡问题(Keyboard遮挡问题)

本篇文章主要介绍了React-Native解决键盘遮挡问题(Keyboard遮挡问题),具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JavaScript反弹动画效果的实现代码

本文通过实例代码给大家介绍了js反弹动画效果的实现代码,需要的朋友参考下吧
收藏 0 赞 0 分享

解决vue2.x中数据渲染以及vuex缓存的问题

本篇文章主要介绍了vue2.x中请求之前数据显示以及vuex缓存的问题,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

jsonp跨域请求详解

这篇文章主要为大家详细介绍了jsonp跨域请求的相关资料,激活了所有接口支持浏览器跨域请求的封装,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

解决vue里碰到 $refs 的问题的方法

本篇文章主要介绍了解决vue里碰到 $refs 的问题的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

js自定义弹框插件的封装

这篇文章主要为大家详细介绍了js自定义弹框插件的简单封装,自己封装一个弹框插件,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

深入理解vue $refs的基本用法

本篇文章主要介绍了深入理解vue $refs的基本用法 ,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多