1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
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>
|