【问题标题】:Determining the sample rate of a large audio file in javascript?在javascript中确定大型音频文件的采样率?
【发布时间】:2015-10-30 05:02:39
【问题描述】:

在 javascript 中,我将数组缓冲区形式的 mp3 数据转换为带有 window.URL.createObjectURL 的对象 URL ...我如何确定 mp3 文件的采样率?

我可以使用网络音频 API 将音频样本加载到内存中,但 mp3 文件为 300MB ...

网络音频 API 能否在不创建 AudioBuffer 的情况下确定音频元素的采样率?

【问题讨论】:

    标签: web-audio-api


    【解决方案1】:

    设置Range 标头以返回足够的字节来读取sampleRate。这仍然会创建一个AudioBuffer,但不会下载整个文件。

    var audioContext = new AudioContext();
    var req = new XMLHttpRequest();
    var url = 'http://example.com/file.mp3';
    req.open('GET', url);
    req.setRequestHeader('Range', 'bytes=0-640'); // enough bytes to read meta data
    req.responseType = 'arraybuffer';
    req.onload = function(event) {
      audioContext.decodeAudioData(this.response, function(decodedBuffer) {
        console.log(decodedBuffer.sampleRate);
      }, function(error) {
        console.log(error);
      });
    };
    req.send();
    

    演示:http://jsfiddle.net/oxv5ykvn/

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    • 2011-01-06
    相关资源
    最近更新 更多