aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/audiocontext/close/index.html
blob: a9a20e8a3a04a769f6e2199c2fa3a784cf96412b (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
---
title: AudioContext.close()
slug: Web/API/AudioContext/close
translation_of: Web/API/AudioContext/close
---
<p>{{ APIRef("Web Audio API") }}</p>

<p>{{ domxref("AudioContext") }}<code>close()</code>方法可以关闭audio context,同时释放占用的所有系统资源。</p>

<p>关闭的context不能用来创建新节点,但可以解码音频数据,创建buffer等等</p>

<p>该函数不会自动释放所有用<code>AudioContext创建的对象</code>,除非其他引用也都已经解除了。但是,它会强制释放所有可能阻止其它<code style="font-style: normal;">AudioContexts被创建或使用</code>的系统音频资源。挂起audio context中音频时间的进度,并停止对音频数据的处理。<span title="The returned 'Promise' resolves when all AudioContext-creation-blocking resources have been released.">所有的<span style="font-family: courier,andale mono,monospace;">AudioContext创建/阻塞资源都被释放后,</span>返回的{{jsxref("Promise")}}才会被释放。</span>如果在一个{{domxref("OfflineAudioContext")}}上调用该方法,则会抛出<code>INVALID_STATE_ERR</code> 异常。</p>

<h2 id="语法">语法</h2>

<pre class="brush: js">var audioCtx = new AudioContext();
audioCtx.close().then(function() { ... });
</pre>

<h3 id="返回值">返回值</h3>

<p>一个 resolve  void值得 {{jsxref("Promise")}}。 </p>

<h2 id="例子">例子</h2>

<p>下面这段代码是<a href="https://github.com/mdn/audiocontext-states/settings">AudioContext states demo</a> (<a href="http://mdn.github.io/audiocontext-states/">直接运行</a>)中的,点击停止按钮调用<code>close()</code>。promise释放后,回到初始状态。</p>

<pre class="brush: js">stopBtn.onclick = function() {
  audioCtx.close().then(function() {
    startBtn.removeAttribute('disabled');
    susresBtn.setAttribute('disabled','disabled');
    stopBtn.setAttribute('disabled','disabled');
  });
}
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">注释</th>
  </tr>
  <tr>
   <td>{{SpecName('Web Audio API', '#widl-AudioContext-close-Promise-void', 'close()')}}</td>
   <td>{{Spec2('Web Audio API')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

{{Compat("api.AudioContext.close")}}

<h2 id="另见">另见</h2>

<ul>
 <li><a href="/en-US/docs/Web_Audio_API/Using_Web_Audio_API">Using the Web Audio API</a></li>
 <li><a href="/en-US/docs/Web/API/Web_Audio_API">Web Audio API</a></li>
</ul>