0
  var audioCtx = new AudioContext();
  function getData() {
      request = new XMLHttpRequest();
      request.open('GET', '4.wav', true);
      request.responseType = 'arraybuffer';
      request.onload = function() {
        var audioData = request.response;
        audioCtx.decodeAudioData(audioData, function(buffer) {
          console.log(buffer)
        });
      }
      request.send();
  }
  getData();

通过以上方法获得到的sampleRate每次都是48000. 怎么获取音频的采样率呢?

xuriliang 119
2019-04-08 提问
1 个回答
0

已采纳

因为我主要是判断wav格式的录音,所以可以通过录音的字节内容来判断:

var audioCtx = new AudioContext();
      function getData() {
          request = new XMLHttpRequest();
          request.open('GET', '3.wav', true);
          request.responseType = 'arraybuffer';
          request.onload = function() {
            let audioData = request.response;
                intArr = new Int8Array(audioData),
                channelArr = intArr.slice(22,24),
                codeArr = intArr.slice(20,22),
                sampleRateArr = intArr.slice(24,28),
                bitPerSampleArr = intArr.slice(34,36);

            let type = String.fromCharCode.apply(null,intArr.slice(8,16)),
                numberOfChannels = ((channelArr[0] & 0xFF) | ((channelArr[1] & 0xFF)<<8)),
                code = ((codeArr[0] & 0xFF) | ((codeArr[1] & 0xFF)<<8)),
                sampleRate = ((sampleRateArr[0] & 0xFF) | ((sampleRateArr[1] & 0xFF)<<8) | ((sampleRateArr[2] & 0xFF)<<16) | ((sampleRateArr[3] & 0xFF)<<24)),
                bitPerSample = ((bitPerSampleArr[0] & 0xFF) | ((bitPerSampleArr[1] & 0xFF)<<8));
            console.log('格式:'+ type + ' 声道数:' + numberOfChannels +' 编码:'+code + ' 采样率:'+sampleRate +' 采样位数:'+bitPerSample);

            /*audioCtx.decodeAudioData(audioData, function(buffer) {
              console.log(buffer)
            });*/
          }
          request.send();
      }
      getData();

参考资料:
https://blog.csdn.net/gwhcsdn...

撰写答案

推广链接