blob: eb34ae11b0120f4e9d945e464728030c2a3926d6 (
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
|
---
title: AudioContext.resume()
slug: Web/API/AudioContext/resume
tags:
- AudioContext
- Web Audio API
- resume
translation_of: Web/API/AudioContext/resume
---
<p>{{ APIRef("Web Audio API") }}</p>
<p>{{ domxref("AudioContext") }} 的 <code>resume()</code> 方法,恢复之前暂停播放的音频。</p>
<p>如果在{{domxref("OfflineAudioContext")}}上调用,会导致<code>INVALID_STATE_ERR错误。</code></p>
<h2 id="语法">语法</h2>
<pre class="brush: js">var audioCtx = new AudioContext();
audioCtx.resume().then(function() { ... });
</pre>
<h3 id="结果">结果</h3>
<p>{{jsxref("Promise")}}成功的话返回空值,返回失败是因为context已经关闭了。</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/">see it running live</a>)的一部分。当点击暂停/恢复按钮的时候,需要{{domxref("AudioContext.state")}}做判断:如果是运行状态,调用{{domxref("suspend()")}},如果是暂停状态,调用<code>resume()。每次点击事件成功后,按钮的文字也会随着变成对应的状态</code>。</p>
<pre class="brush: js">susresBtn.onclick = function() {
if(audioCtx.state === 'running') {
audioCtx.suspend().then(function() {
susresBtn.textContent = 'Resume context';
});
} else if(audioCtx.state === 'suspended') {
audioCtx.resume().then(function() {
susresBtn.textContent = 'Suspend context';
});
}
}
</pre>
<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', '#widl-AudioContext-resume-Promise-void', 'close()')}}</td>
<td>{{Spec2('Web Audio API')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="兼容性">兼容性</h2>
{{Compat("api.AudioContext.resume")}}
<h2 id="参见">参见</h2>
<ul>
<li><a href="/zh-CN/docs/Web/API/Web_Audio_API/Using_Web_Audio_API">Using the Web Audio API</a></li>
<li><a href="/zh-CN/docs/Web/API/Web_Audio_API">Web Audio API</a></li>
</ul>
|