AudioContext接口的createMediaStreamDestination()方法用于创建一个新的对象,该对象关联着表示音频流的一个 WebRTC MediaStream ,音频流可以存储在本地文件或者被发送到另外一台计算机.

The MediaStream is created when the node is created and is accessible via the MediaStreamAudioDestinationNode's stream attribute. This stream can be used in a similar way as a MediaStream obtained via navigator.getUserMedia — it can, for example, be sent to a remote peer using the RTCPeerConnection addStream() method.

For more details about media stream destination nodes, check out the MediaStreamAudioDestinationNode reference page.


var audioCtx = new AudioContext();
var destination = audioCtx.createMediaStreamDestination();


A MediaStreamAudioDestinationNode.


In the following simple example, we create a MediaStreamAudioDestinationNode, an OscillatorNode and a MediaRecorder (the example will therefore only work in Firefox at this time.) The MediaRecorder is set up to record information from the MediaStreamDestinationNode.

When the button is clicked, the oscillator starts, and the MediaRecorder is started. When the button is stopped, the oscillator and MediaRecorder both stop. Stopping the MediaRecorder causes the dataavailable event to fire, and the event data is pushed into the chunks array. After that, the stop event fires, a new blob is made of type opus — which contains the data in the chunks array, and a new window (tab) is then opened that points to a URL created from the blob.

From here, you can play and save the opus file.

<!DOCTYPE html>
    <title>createMediaStreamDestination() demo</title>
    <h1>createMediaStreamDestination() demo</h1>

    <p>Encoding a pure sine wave to an Opus file </p>
    <button>Make sine wave</button>
    <audio controls></audio>
     var b = document.querySelector("button");
     var clicked = false;
     var chunks = [];
     var ac = new AudioContext();
     var osc = ac.createOscillator();
     var dest = ac.createMediaStreamDestination();
     var mediaRecorder = new MediaRecorder(;

     b.addEventListener("click", function(e) {
       if (!clicked) {
  = "Stop recording";
           clicked = true;
         } else {
  = true;

     mediaRecorder.ondataavailable = function(evt) {
       // push each chunk (blobs) in an array

     mediaRecorder.onstop = function(evt) {
       // Make blob out of our blobs, and open it.
       var blob = new Blob(chunks, { 'type' : 'audio/ogg; codecs=opus' });
       document.querySelector("audio").src = URL.createObjectURL(blob);

Note: You can view this example live, or study the source code, on Github.


Specification Status Comment
Web Audio API
Working Draft  

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 10.0webkit 25.0 (25.0)  未实现 15.0webkit
22 (unprefixed)
Feature Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? 26.0 1.2 ? ? ? 33.0

See also


 此页面的贡献者: huangxok
 最后编辑者: huangxok,