# Audio Recording Development ## When to Use During audio recording, audio signals are recorded and saved. You can set the sampling rate, number of audio channels, bit rate, encoding format, encapsulation format, and file path for audio recording. ## Available APIs **Table 1** APIs for audio recording

API

Description

media.createAudioRecorder()

Creates an AudioRecorder instance.

AudioRecorder

Provides audio recording methods.

AudioRecorderConfig

Provides parameters related to audio recording.

**Table 2** AudioRecorder methods

Method

Description

prepare(config: AudioRecorderConfig): void

Prepares for audio recording and sets parameters.

start(): void

Starts audio recording.

pause(): void

Pauses audio recording. (Not supported yet)

resume(): void

Resumes audio recording. (Not supported yet)

stop(): void

Stops audio recording.

release(): void

Releases audio recording resources.

reset(): void

Resets audio recording.

on('prepare', function callback)

Subscribes to the audio recording preparation event.

on('start', function callback)

Subscribes to the audio recording start event.

on('pause', function callback)

Subscribes to the audio recording pause event.

on('resume', function callback)

Subscribes to the audio recording resumption event.

on('stop',function callback)

Subscribes to the audio recording end event.

on('release', function callback)

Subscribes to the event of releasing audio recording resources.

on('reset', function callback)

Subscribes to the audio recording reset event.

on('error', function callback)

Subscribes to the audio recording error event.

**Table 3** AudioRecorderConfig

API

Description

audioEncoder?: AudioEncoder

Audio encoding format. The default value is AAC_LC.

audioEncodeBitRate?: number

Bit rate for audio encoding. The default value is 48000.

audioSampleRate?: number

Sampling rate for audio encoding. The default value is 48000.

numberOfChannels?: number

Number of audio channels. The default value is 2.

format?: AudioOutputFormat

Audio output format. The default value is MPEG_4.

uri: string

Audio output path, which starts with file://path or file://fd.

**Table 4** AudioEncoder

API

Description

AAC_LC = 3

AAC_LC format.

**Table 5** AudioOutputFormat

API

Description

MPEG_4 = 2

MPEG-4 format.

AAC_ADTS= 6

AAC_ADTS format.

1. Create an audio recorder. ``` import media from '@ohos.multimedia.media'; var recorder = media.createAudioRecorder(); ``` 2. Prepare for audio recording and set parameters. ``` let audioRecorderConfig = { audioEncoder : AAC_LC , audioEncodeBitRate : 22050, audioSampleRate : 22050, numberOfChannels : 2, format : AAC_ADTS, uri : 'file:///data/accounts/account_0/appdata/appdata/recorder/test.m4a', } recorder.prepare(audioRecorderConfig); ``` 3. Set the subscription events. ``` recorder.on('prepare', () => { console.info('setCallback prepare() case callback is called'); recorder.start(); }); recorder.on('start', () => { console.info('setCallback start() case callback is called'); setTimeout(function(){ recorder.pause(); }, 10000); // Pause recording 10 seconds after the recording starts. }); recorder.on('pause', () => { console.info('setCallback pause() case callback is called'); setTimeout(function(){ recorder.resume(); }, 10000); // Resume recording 10 seconds after the recording is paused. }); recorder.on('resume', () => { console.info('setCallback resume() case callback is called'); setTimeout(function(){ recorder.stop(); }, 10000); // Stop recording 10 seconds after the recording is resumed. }); recorder.on('stop', () => { console.info('setCallback stop() case callback is called'); recorder.release(); }); recorder.on('release', () => { console.info('setCallback release() case callback is called'); }); recorder.on('error', (err) => { console.info(`case error called,errCode is ${err.code}`); console.info(`case error called,errMessage is ${err.message}`); recorder.reset(); }); ```