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
| Property | Type | Required | DefaultValue | Description | API_LEVEL |
|---|---|---|---|---|---|
| START | number | Y | - | Result reported after recording starts | 3.0 |
| STOP | number | Y | - | Recording stopped | 3.0 |
state
| Property | Type | Required | DefaultValue | Description | API_LEVEL |
|---|---|---|---|---|---|
| IDLE | number | Y | - | Initial state | 3.0 |
| PREPARING | number | Y | - | Requesting recording resources | 3.0 |
| STARTING | number | Y | - | Starting recording | 3.0 |
| RECORDING | number | Y | - | Recording in progress | 3.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()