blob: e811ad5188b2293095c5ac38b370e29084402cdc (
plain)
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
|
---
title: OfflineAudioContext.OfflineAudioContext()
slug: Web/API/OfflineAudioContext/OfflineAudioContext
translation_of: Web/API/OfflineAudioContext/OfflineAudioContext
---
<p>{{APIRef("Web Audio API")}}</p>
<p><code><strong>OfflineAudioContext()</strong></code> 构造函数创建一个新的 {{domxref("OfflineAudioContext")}} 对象实例。</p>
<h2 id="语法">语法</h2>
<pre class="brush: js">var myOfflineAudio = new OfflineAudioContext(numOfChannels,length,sampleRate);</pre>
<h3 id="参数">参数</h3>
<dl>
<dt>numOfChannels</dt>
<dd>An integer 代表该缓冲区拥有的声道的数目。该实现需要支持至少32个声道。</dd>
<dt>length</dt>
<dd>代表采样帧缓冲区的大小的整数。</dd>
<dt>sampleRate</dt>
<dd>采样帧每一秒的线性音频数据的采样率。该实现必须支持在 22050 到 96000 之间的采样率,44100 是最经常用到的采样率。</dd>
</dl>
<p>这里有个重要的警告,你可以通过不带参数的使用 <code>new AudioContext()</code> 构造函数创建一个新的 {{domxref("AudioContext")}},但是 <code>OfflineAudioContext()</code> 构造函数必须带上三个参数。当你通过 {{domxref("AudioContext.createBuffer")}} 方法创建一个新的{{domxref("AudioBuffer")}} 时,你也是需要做一样的事情。想要知道更多信息,请阅读我们的基本概念指南的 <a href="/zh-CN/docs/Web/API/Web_Audio_API/Basic_concepts_behind_Web_Audio_API#音频片段:帧,样本和声道">音频片段:帧,样本和声道</a>。</p>
<div class="note">
<p><strong>注意</strong>:像普通的 <code>AudioContext</code>,<code>OfflineAudioContext</code> 可以成为事件的目标,因此它的实现是 {{domxref("EventTarget")}} 接口。</p>
</div>
<h2 id="例子">例子</h2>
<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="comment token">// 定义一个在线或者离线的音频上下文</span>
<span class="keyword token">var</span> audioCtx <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">AudioContext</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
<span class="keyword token">var</span> offlineCtx <span class="operator token">=</span> <span class="keyword token">new</span> <span class="class-name token">OfflineAudioContext</span><span class="punctuation token">(</span><span class="number token">2</span><span class="punctuation token">,</span><span class="number token">44100</span><span class="operator token">*</span><span class="number token">40</span><span class="punctuation token">,</span><span class="number token">44100</span><span class="punctuation token">)</span><span class="punctuation token">;</span>
source <span class="operator token">=</span> offlineCtx<span class="punctuation token">.</span><span class="function token">createBufferSource</span><span class="punctuation token">(</span><span class="punctuation token">)</span><span class="punctuation token">;
// 更多代码...</span></code>
</pre>
<div class="note">
<p><strong>注意</strong>: 想要获取完整的例子,请看我们在 Github 仓库的 <a class="external external-icon" href="http://mdn.github.io/offline-audio-context-promise/">offline-audio-context-promise</a> (也可以看 <a class="external external-icon" href="https://github.com/mdn/offline-audio-context-promise">源代码</a> )</p>
</div>
<h2 id="标准">标准</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','#OfflineAudioContext','OfflineAudioContext')}}</td>
<td>{{Spec2('Web Audio API')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<p>{{ CompatibilityTable() }}</p>
<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>{{CompatVersionUnknown}}<br>
</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{ CompatNo }}</td>
<td>
<p>{{CompatVersionUnknown}}</p>
</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Android Webview</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Phone</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
<th>Chrome for Android</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{ CompatNo }}</td>
<td>{{CompatVersionUnknown}}<br>
</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{ CompatNo }}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{ CompatNo }}</td>
<td>{{CompatVersionUnknown}}</td>
</tr>
</tbody>
</table>
</div>
|