Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

WeChat small program API recording and recording manager


May 18, 2021 WeChat Mini Program Development Document


Table of contents


RecorderManager

Globally unique recording manager


Method:

RecorderManager.onError(function callback)

Listen for recording error events

Parameters

function callback

The callback function that recorded the error event

Parameters

Object res
Property Type Description
errMsg string The error message


RecorderManager.onFrameRecorded(function callback)

Listen for file events that have finished recording the specified frame size. If frameSize is set, this event is recalled.

Parameters

function callback

Compatible processing of file events that have been recorded at the specified frame size.

Parameters

Object res
Property Type Description
frameBuffer ArrayBuffer Recorded shrapned data
isLastFrame boolean The last frame before the end of the current frame's normal recording


RecorderManager.onInterruptionBegin(function callback)

The base library 2.3.0 starts to support, and the lower version needs to be compatible.

Listening to recordings is interrupted by the start event because it is occupied by the system. T his event is triggered by the following scenarios: WeChat voice chat, WeChat video chat. W hen this event is triggered, the recording is paused. The pause event is triggered after this event

Parameters

function callback

The callback function that interrupts the start event because the recording is occupied by the system


RecorderManager.onInterruptionEnd(function callback)

The base library 2.3.0 starts to support, and the lower version needs to be compatible.

Listen to the recording interrupt end event. After receiving the sessionBegin event, all recordings within the program are paused until the event is received.

Parameters

function callback

The callback function for the recording interrupt end event


RecorderManager.onPause(function callback)

Listen for recording pause events

Parameters

function callback

The callback function for recording pause events


RecorderManager.onResume(function callback)

Listen to the recording to continue the event

Parameters

function callback

The callback function of the recording continues the event


RecorderManager.onStart(function callback)

Listen to the recording to start the event

Parameters

function callback

The callback function for recording the start event


RecorderManager.onStop(function callback)

Listen to the recording to end the event

Parameters

function callback

The callback function for recording the end event

Parameters

Object res
Property Type Description
tempFilePath string Temporary path to recording file (local path)
duration number Total recording time, in ms
fileSize number Record file size, in Byte


RecorderManager.pause()

Pause the recording


RecorderManager.resume()

Continue recording


RecorderManager.start(Object object)

Start recording

Parameters

Object object

Property Type The default Required Description The lowest version
duration number 60000 Whether Recording length, ms, maximum 600000 (10 minutes)
sampleRate number 8000 Whether The sample rate
numberOfChannels number 2 Whether The number of recording channels
encodeBitRate number 48000 Whether Code rate, valid value can be found in the table below
format string Aac Whether Audio format
frameSize number Whether Specify the frame size in KB. W hen frameSize is passed in, the recorded file content is thrased for each recording of the specified frame size, and no callback is made without specifying it. Only the mp3 format is currently supported.
audioSource string auto Whether Specify the audio input source for the recording, and you can get the currently available audio source through wx.getAvailableAudioSources(). 2.1.0

The legal value of object.sampleRate

Value Description The lowest version
8000 8000 sample rate
11025 11025 sample rate
12000 12000 sample rate
16000 16000 sample rate
22050 22050 sample rate
24000 24000 sample rate
32000 32000 sample rate
44100 44100 sample rate
48000 48000 sample rate

The legal value of object.numberOfChannels

Value Description The lowest version
1 1 channel
2 2 channels

The legal value of object.format

Value Description The lowest version
mp3 mp3 format
Aac aac format
Wav Wav format
Pcm pcm format

The legal value of object.audioSource

Value Description The lowest version
auto Automatic settings, the default use of mobile phone microphone, plug in the headset and automatically switch the use of headphone microphone, all platforms apply
buildInMic Mobile microphone, iOS only
headsetMic Headset microphone, iOS only
Mic Microphones (phone microphones when you don't have a headset, headphone microphones when you plug in your headset), Android only
camcorder Same mic for recording audio and video content, Android only
voice_communication With mic, for real-time communication, Android only
voice_recognition Same mic, for speech recognition, Android only

Sample rate and code rate limits

Each sample rate has a valid value for the code rate range, and setting an illegal sample rate or code rate can cause the recording to fail, as shown in the table below.

The sample rate Code rate
8000 16000 ~ 48000
11025 16000 ~ 48000
12000 24000 ~ 64000
16000 24000 ~ 96000
22050 32000 ~ 128000
24000 32000 ~ 128000
32000 48000 ~ 192000
44100 64000 ~ 320000
48000 64000 ~ 320000


RecorderManager.stop()

Stop recording



The sample code

const recorderManager = wx.getRecorderManager()

recorderManager.onStart(() => {
  console.log('recorder start')
})
recorderManager.onPause(() => {
  console.log('recorder pause')
})
recorderManager.onStop((res) => {
  console.log('recorder stop', res)
  const { tempFilePath } = res
})
recorderManager.onFrameRecorded((res) => {
  const { frameBuffer } = res
  console.log('frameBuffer.byteLength', frameBuffer.byteLength)
})

const options = {
  duration: 10000,
  sampleRate: 44100,
  numberOfChannels: 1,
  encodeBitRate: 192000,
  format: 'aac',
  frameSize: 50
}

recorderManager.start(options)