Skip to main content
Version: v3+

Recorder

Start from API_LEVEL 3.0 . Please refer to API_LEVEL.

The media recorder controller records audio to a file in the mini program data directory..

Properties

event

PropertyTypeRequiredDefaultValueDescriptionAPI_LEVEL
STARTnumberY-Result reported after recording starts3.0
STOPnumberY-Recording stopped3.0

state

PropertyTypeRequiredDefaultValueDescriptionAPI_LEVEL
IDLEnumberY-Initial state3.0
PREPARINGnumberY-Requesting recording resources3.0
STARTINGnumberY-Starting recording3.0
RECORDINGnumberY-Recording in progress3.0

Methods

setFormat

Set the recording format and output file. Use a value from codec; codec.OPUS is currently supported. options.target_file specifies the output path in the mini program data directory, for example data://record_file.opus

setFormat(codecValue: typeof codec.OPUS, options: { target_file: string }): void

start

Start recording

start(): void

stop

Stop recording

stop(): void

getStatus

Get the recorder state; see recorder.state for the meanings

getStatus(): number

addEventListener

Listen for recorder state changes; callback is triggered when the state changes

addEventListener(event: number, callback: (result: boolean | undefined) => void): void

Example

import { codec, create, id } from '@zos/media'

const recorder = create(id.RECORDER)
recorder.addEventListener(recorder.event.START, (result) => {
if (result) console.log('recording started')
})
recorder.addEventListener(recorder.event.STOP, () => {
console.log('recording stopped')
})
recorder.setFormat(codec.OPUS, {
target_file: 'data://record.opus',
})
recorder.start()
recorder.getStatus()
recorder.stop()