diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/audiocontext/createmediastreamdestination | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/audiocontext/createmediastreamdestination')
-rw-r--r-- | files/zh-cn/web/api/audiocontext/createmediastreamdestination/index.html | 161 |
1 files changed, 161 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/audiocontext/createmediastreamdestination/index.html b/files/zh-cn/web/api/audiocontext/createmediastreamdestination/index.html new file mode 100644 index 0000000000..c934a2fd2d --- /dev/null +++ b/files/zh-cn/web/api/audiocontext/createmediastreamdestination/index.html @@ -0,0 +1,161 @@ +--- +title: AudioContext.createMediaStreamDestination() +slug: Web/API/AudioContext/createMediaStreamDestination +translation_of: Web/API/AudioContext/createMediaStreamDestination +--- +<p>{{ APIRef("Web Audio API") }}</p> + +<div> +<p>{{ domxref("AudioContext") }}接口的<code>createMediaStreamDestination()方法用于创建一个新的对象,该对象关联着表示音频流的一个</code> <a href="/en-US/docs/WebRTC">WebRTC</a> {{domxref("MediaStream")}} ,音频流可以存储在本地文件或者被发送到另外一台计算机.</p> +</div> + +<p>The {{domxref("MediaStream")}} is created when the node is created and is accessible via the {{domxref("MediaStreamAudioDestinationNode")}}'s <code>strea<dfn>m</dfn></code> attribute. This stream can be used in a similar way as a <code>MediaStream</code> obtained via {{domxref("navigator.getUserMedia") }} — it can, for example, be sent to a remote peer using the <code>RTCPeerConnection</code> <code>addStream()</code> method.</p> + +<p>For more details about media stream destination nodes, check out the {{domxref("MediaStreamAudioDestinationNode")}} reference page.</p> + +<h2 id="语法">语法</h2> + +<pre class="brush: js">var audioCtx = new AudioContext(); +var destination = audioCtx.createMediaStreamDestination();</pre> + +<h3 id="返回值">返回值</h3> + +<p>A {{domxref("MediaStreamAudioDestinationNode")}}.</p> + +<h2 id="Example">Example</h2> + +<p>In the following simple example, we create a {{domxref("MediaStreamAudioDestinationNode")}}, an {{ domxref("OscillatorNode") }} and a {{ domxref("MediaRecorder") }} (the example will therefore only work in Firefox at this time.) The <code>MediaRecorder</code> is set up to record information from the <code>MediaStreamDestinationNode</code>.</p> + +<p>When the button is clicked, the oscillator starts, and the <code>MediaRecorder</code> is started. When the button is stopped, the oscillator and<code> MediaRecorder</code> both stop. Stopping the <code>MediaRecorder</code> causes the <code>dataavailable</code> event to fire, and the event data is pushed into the <code>chunks</code> array. After that, the <code>stop</code> event fires, a new <code>blob</code> is made of type opus — which contains the data in the <code>chunks</code> array, and a new window (tab) is then opened that points to a URL created from the blob.</p> + +<p>From here, you can play and save the opus file.</p> + +<pre class="brush: html;highlight[17]"><!DOCTYPE html> +<html> + <head> + <title>createMediaStreamDestination() demo</title> + </head> + <body> + <h1>createMediaStreamDestination() demo</h1> + + <p>Encoding a pure sine wave to an Opus file </p> + <button>Make sine wave</button> + <audio controls></audio> + <script> + 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(dest.stream); + osc.connect(dest); + + b.addEventListener("click", function(e) { + if (!clicked) { + mediaRecorder.start(); + osc.start(0); + e.target.innerHTML = "Stop recording"; + clicked = true; + } else { + mediaRecorder.stop(); + osc.stop(0); + e.target.disabled = true; + } + }); + + mediaRecorder.ondataavailable = function(evt) { + // push each chunk (blobs) in an array + chunks.push(evt.data); + }; + + 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); + }; + </script> + </body> +</html></pre> + +<div class="note"> +<p><strong>Note</strong>: You can <a href="https://mdn.github.io/webaudio-examples/create-media-stream-destination/index.html">view this example live</a>, or <a href="https://github.com/mdn/webaudio-examples/blob/master/create-media-stream-destination/index.html">study the source code</a>, on Github.</p> +</div> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('Web Audio API', '#widl-AudioContext-createMediaStreamDestination-MediaStreamAudioDestinationNode', 'createMediaStreamDestination()')}}</td> + <td>{{Spec2('Web Audio API')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(10.0)}}{{property_prefix("webkit")}}</td> + <td>{{CompatGeckoDesktop(25.0)}} </td> + <td>{{CompatNo}}</td> + <td>15.0{{property_prefix("webkit")}}<br> + 22 (unprefixed)</td> + <td>6.0{{property_prefix("webkit")}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatUnknown}}</td> + <td>26.0</td> + <td>1.2</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>33.0</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="/en-US/docs/Web_Audio_API/Using_Web_Audio_API">Using the Web Audio API</a></li> +</ul> |