decodeAudioData() 方法在 BaseAudioContext Interface is used to asynchronously decode audio file data contained in an ArrayBuffer . In this case the ArrayBuffer is loaded from XMLHttpRequest and FileReader . The decoded AudioBuffer is resampled to the AudioContext 's sampling rate, then passed to a callback or promise.

This is the preferred method of creating an audio source for Web Audio API from an audio track. This method only works on complete file data, not fragments of audio file data.

句法

Older callback syntax:

baseAudioContext.decodeAudioData(ArrayBuffer, successCallback, errorCallback);
					

Newer promise-based syntax:

Promise<decodedData> baseAudioContext.decodeAudioData(ArrayBuffer);
					

参数

ArrayBuffer
An ArrayBuffer containing the audio data to be decoded, usually grabbed from XMLHttpRequest , WindowOrWorkerGlobalScope.fetch() or FileReader .
successCallback
A callback function to be invoked when the decoding successfully finishes. The single argument to this callback is an AudioBuffer 表示 decodedData (the decoded PCM audio data). Usually you'll want to put the decoded data into an AudioBufferSourceNode , from which it can be played and manipulated how you want.
errorCallback

An optional error callback, to be invoked if an error occurs when the audio data is being decoded.

返回值

Void, or a Promise object that fulfills with the decodedData .

范例

In this section we will first cover the older callback-based system and then the newer promise-based syntax.

Older callback syntax

在此范例中, getData() function uses XHR to load an audio track, setting the responseType of the request to arraybuffer so that it returns an array buffer as its response that we then store in the audioData variable . We then pass this buffer into a decodeAudioData() function; the success callback takes the successfully decoded PCM data, puts it into an AudioBufferSourceNode created using AudioContext.createBufferSource() , connects the source to the AudioContext.destination and sets it to loop.

The buttons in the example simply run getData() to load the track and start it playing, and stop it playing, respectively. When the stop() method is called on the source, the source is cleared out.

注意 : You can run the example live (或 view the source )。

// define variables
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
var source;
var pre = document.querySelector('pre');
var myScript = document.querySelector('script');
var play = document.querySelector('.play');
var stop = document.querySelector('.stop');
// use XHR to load an audio track, and
// decodeAudioData to decode it and stick it in a buffer.
// Then we put the buffer into the source
function getData() {
  source = audioCtx.createBufferSource();
  var request = new XMLHttpRequest();
  request.open('GET', 'viper.ogg', true);
  request.responseType = 'arraybuffer';
  request.onload = function() {
    var audioData = request.response;
    audioCtx.decodeAudioData(audioData, function(buffer) {
        source.buffer = buffer;
        source.connect(audioCtx.destination);
        source.loop = true;
      },
      function(e){ console.log("Error with decoding audio data" + e.err); });
  }
  request.send();
}
// wire up buttons to stop and play audio
play.onclick = function() {
  getData();
  source.start(0);
  play.setAttribute('disabled', 'disabled');
}
stop.onclick = function() {
  source.stop(0);
  play.removeAttribute('disabled');
}
// dump script to pre element
pre.innerHTML = myScript.innerHTML;
					

New promise-based syntax

ctx.decodeAudioData(audioData).then(function(decodedData) {
 // use the decoded data here
});
					

规范

规范 状态 注释
Web 音频 API
The definition of 'decodeAudioData()' in that specification.
工作草案

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
decodeAudioData Chrome 10 Prefixed
10 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Edge ≤18 Firefox 53 注意事项
53 注意事项
Originally implemented on AudioContext in Firefox 25.
IE 不支持 No Opera 22
22
15 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari 6 Prefixed
6 Prefixed
Prefixed Implemented with the vendor prefix: webkit
WebView Android Yes Chrome Android 33 Firefox Android 53 注意事项
53 注意事项
Originally implemented on AudioContext in Firefox Android 26.
Opera Android 22
22
14 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Safari iOS 6 Prefixed
6 Prefixed
Prefixed Implemented with the vendor prefix: webkit
Samsung Internet Android 2.0
Promise-based syntax Chrome 49 Edge ≤79 Firefox 53 注意事项
53 注意事项
Originally implemented on AudioContext in Firefox 36.
IE 不支持 No Opera Yes Safari 不支持 No WebView Android 49 Chrome Android 49 Firefox Android 53 注意事项
53 注意事项
Originally implemented on AudioContext in Firefox Android 36.
Opera Android ? Safari iOS 不支持 No Samsung Internet Android 5.0

图例

完整支持

完整支持

不支持

不支持

兼容性未知 ?

兼容性未知

见实现注意事项。

要求使用供应商前缀或不同名称。

要求使用供应商前缀或不同名称。

另请参阅

元数据

  • 最后修改:
  1. Web 音频 API
  2. BaseAudioContext
  3. 特性
    1. audioWorklet
    2. currentTime
    3. destination
    4. listener
    5. sampleRate
    6. state
  4. 方法
    1. createAnalyser()
    2. createBiquadFilter()
    3. createBuffer()
    4. createBufferSource()
    5. createChannelMerger()
    6. createChannelSplitter()
    7. createConstantSource()
    8. createConvolver()
    9. createDelay()
    10. createDynamicsCompressor()
    11. createGain()
    12. createIIRFilter()
    13. createOscillator()
    14. createPanner()
    15. createPeriodicWave()
    16. createScriptProcessor()
    17. createStereoPanner()
    18. createWaveShaper()
    19. decodeAudioData()
  5. 继承:
    1. EventTarget
  6. Related pages for Web Audio API
    1. AnalyserNode
    2. AudioBuffer
    3. AudioBufferSourceNode
    4. AudioContext
    5. AudioContextOptions
    6. AudioDestinationNode
    7. AudioListener
    8. AudioNode
    9. AudioNodeOptions
    10. AudioParam
    11. AudioProcessingEvent
    12. AudioScheduledSourceNode
    13. AudioWorklet
    14. AudioWorkletGlobalScope
    15. AudioWorkletNode
    16. AudioWorkletProcessor
    17. BiquadFilterNode
    18. ChannelMergerNode
    19. ChannelSplitterNode
    20. ConstantSourceNode
    21. ConvolverNode
    22. DelayNode
    23. DynamicsCompressorNode
    24. GainNode
    25. IIRFilterNode
    26. MediaElementAudioSourceNode
    27. MediaStreamAudioDestinationNode
    28. MediaStreamAudioSourceNode
    29. OfflineAudioCompletionEvent
    30. OfflineAudioContext
    31. OscillatorNode
    32. PannerNode
    33. PeriodicWave
    34. StereoPannerNode
    35. WaveShaperNode

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1