多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# .start ### RecorderManager.start(Object object) 开始录音 #### 参数 ##### Object object 属性类型默认值是否必填说明支持版本durationnumber60000否录音的时长,单位 ms,最大值 600000(10 分钟)sampleRatenumber8000否采样率numberOfChannelsnumber2否录音通道数encodeBitRatenumber48000否编码码率,有效值见下表格formatstringaac否音频格式frameSizenumber否指定帧大小,单位 KB。传入 frameSize 后,每录制指定帧大小的内容后,会回调录制的文件内容,不指定则不会回调。暂仅支持 mp3 格式。audioSourcestringauto否指定录音的音频输入源,可通过 [wx.getAvailableAudioSources()](/read/wechat-game-api/98.md) 获取当前可用的音频源>= 2.1.0**object.sampleRate 的合法值** 值说明80008000 采样率1102511025 采样率1200012000 采样率1600016000 采样率2205022050 采样率2400024000 采样率3200032000 采样率4410044100 采样率4800048000 采样率**object.numberOfChannels 的合法值** 值说明11 个通道22 个通道**object.format 的合法值** 值说明mp3mp3 格式aacaac 格式**object.audioSource 的合法值** 值说明auto自动设置,默认使用手机麦克风,插上耳麦后自动切换使用耳机麦克风,所有平台适用buildInMic手机麦克风,仅限 iOSheadsetMic耳机麦克风,仅限 iOSmic麦克风(没插耳麦时是手机麦克风,插耳麦时是耳机麦克风),仅限 Androidcamcorder同 mic,适用于录制音视频内容,仅限 Androidvoice\_communication同 mic,适用于实时沟通,仅限 Androidvoice\_recognition同 mic,适用于语音识别,仅限 Android#### 采样率与编码码率限制 每种采样率有对应的编码码率范围有效值,设置不合法的采样率或编码码率会导致录音失败,具体对应关系如下表。 采样率编码码率800016000 ~ 480001102516000 ~ 480001200024000 ~ 640001600024000 ~ 960002205032000 ~ 1280002400032000 ~ 1280003200048000 ~ 1920004410064000 ~ 3200004800064000 ~ 320000#### 示例代码 ``` <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> </ol> ``` recorderManager.start(options) > 原文: <https://developers.weixin.qq.com/minigame/dev/api/media/recorder/RecorderManager.start.html>