ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# RecorderManager ### RecorderManager 全局唯一的录音管理器 #### 方法 ##### RecorderManager.start(Object object) 开始录音 ##### RecorderManager.pause() 暂停录音 ##### RecorderManager.resume() 继续录音 ##### RecorderManager.stop() 停止录音 ##### RecorderManager.onStart(function callback) 监听录音开始事件 ##### RecorderManager.onResume(function callback) 监听录音继续事件 ##### RecorderManager.onPause(function callback) 监听录音暂停事件 ##### RecorderManager.onStop(function callback) 监听录音结束事件 ##### RecorderManager.onFrameRecorded(function callback) 监听已录制完指定帧大小的文件事件。如果设置了 frameSize,则会回调此事件。 ##### RecorderManager.onError(function callback) 监听录音错误事件 ##### RecorderManager.onInterruptionBegin(function callback) 监听录音因为受到系统占用而被中断开始事件。以下场景会触发此事件:微信语音聊天、微信视频聊天。此事件触发后,录音会被暂停。pause 事件在此事件后触发 ##### RecorderManager.onInterruptionEnd(function callback) 监听录音中断结束事件。在收到 interruptionBegin 事件之后,小程序内所有录音会暂停,收到此事件之后才可再次录音成功。 #### 示例代码 ``` <pre class="prettyprint"><ol class="linenums"><li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="kwd">const</span><span class="pln"> recorderManager </span><span class="pun">=</span><span class="pln"> wx</span><span class="pun">.</span><span class="pln">getRecorderManager</span><span class="pun">()</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">recorderManager</span><span class="pun">.</span><span class="pln">onStart</span><span class="pun">(</span><span class="hljs-params"><span class="pun">()</span></span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'recorder start'</span><span class="pun">)</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">recorderManager</span><span class="pun">.</span><span class="pln">onPause</span><span class="pun">(</span><span class="hljs-params"><span class="pun">()</span></span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'recorder pause'</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">recorderManager</span><span class="pun">.</span><span class="pln">onStop</span><span class="pun">((</span><span class="hljs-params"><span class="pln">res</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'recorder stop'</span><span class="pun">,</span><span class="pln"> res</span><span class="pun">)</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">const</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> tempFilePath </span><span class="pun">}</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> res</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">recorderManager</span><span class="pun">.</span><span class="pln">onFrameRecorded</span><span class="pun">((</span><span class="hljs-params"><span class="pln">res</span></span><span class="pun">)</span><span class="pln"> </span><span class="pun">=></span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> </span><span class="kwd">const</span><span class="pln"> </span><span class="pun">{</span><span class="pln"> frameBuffer </span><span class="pun">}</span><span class="pln"> </span><span class="pun">=</span><span class="pln"> res</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="str">'frameBuffer.byteLength'</span><span class="pun">,</span><span class="pln"> frameBuffer</span><span class="pun">.</span><span class="pln">byteLength</span><span class="pun">)</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">})</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="kwd">const</span><span class="pln"> options </span><span class="pun">=</span><span class="pln"> </span><span class="pun">{</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> duration</span><span class="pun">:</span><span class="pln"> </span><span class="lit">10000</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> sampleRate</span><span class="pun">:</span><span class="pln"> </span><span class="lit">44100</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> numberOfChannels</span><span class="pun">:</span><span class="pln"> </span><span class="lit">1</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> encodeBitRate</span><span class="pun">:</span><span class="pln"> </span><span class="lit">192000</span><span class="pun">,</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> format</span><span class="pun">:</span><span class="pln"> </span><span class="str">'aac'</span><span class="pun">,</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln"> frameSize</span><span class="pun">:</span><span class="pln"> </span><span class="lit">50</span></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pun">}</span></code></li> <li class="l1"><code class="pcalibre9 pcalibre8 pcalibre7"></code></li> <li class="l"><code class="pcalibre9 pcalibre8 pcalibre7"><span class="pln">recorderManager</span><span class="pun">.</span><span class="pln">start</span><span class="pun">(</span><span class="pln">options</span><span class="pun">)</span></code></li> </ol> ``` > 原文: <https://developers.weixin.qq.com/minigame/dev/api/media/recorder/RecorderManager.html>