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.
During audio recording, audio signals are captured, encoded, and saved to files. You can specify parameters such as the sampling rate, number of audio channels, encoding format, encapsulation format, and file path for audio recording.
## Available APIs<a name="section1225204317126"></a>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p11619510202511"><aname="p11619510202511"></a><aname="p11619510202511"></a>Creates an <strongid="b114853895016"><aname="b114853895016"></a><aname="b114853895016"></a>AudioRecorder</strong> instance.</p>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p1855713186239"><aname="p1855713186239"></a><aname="p1855713186239"></a>Provides parameters related to audio recording.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p45511713162216"><aname="p45511713162216"></a><aname="p45511713162216"></a>Prepares for audio recording and sets parameters.</p>
<trid="row44091647192718"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p18937155862412"><aname="p18937155862412"></a><aname="p18937155862412"></a>on('prepare', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p13409164722717"><aname="p13409164722717"></a><aname="p13409164722717"></a>Subscribes to the audio recording preparation event.</p>
</td>
</tr>
<trid="row12409154711272"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p124091474279"><aname="p124091474279"></a><aname="p124091474279"></a>on('start', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p7409174714274"><aname="p7409174714274"></a><aname="p7409174714274"></a>Subscribes to the audio recording start event.</p>
</td>
</tr>
<trid="row040917477273"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p18981171411254"><aname="p18981171411254"></a><aname="p18981171411254"></a>on('pause', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p1409164772714"><aname="p1409164772714"></a><aname="p1409164772714"></a>Subscribes to the audio recording pause event.</p>
</td>
</tr>
<trid="row712184163016"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p1167517115311"><aname="p1167517115311"></a><aname="p1167517115311"></a>on('resume', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p1113134115301"><aname="p1113134115301"></a><aname="p1113134115301"></a>Subscribes to the audio recording resumption event.</p>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p8410124742713"><aname="p8410124742713"></a><aname="p8410124742713"></a>Subscribes to the audio recording end event.</p>
</td>
</tr>
<trid="row64105474278"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p166588276253"><aname="p166588276253"></a><aname="p166588276253"></a>on('release', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p54101047102712"><aname="p54101047102712"></a><aname="p54101047102712"></a>Subscribes to the event of releasing audio recording resources.</p>
</td>
</tr>
<trid="row1410154715273"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p135217361257"><aname="p135217361257"></a><aname="p135217361257"></a>on('reset', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p1541054719271"><aname="p1541054719271"></a><aname="p1541054719271"></a>Subscribes to the audio recording reset event.</p>
</td>
</tr>
<trid="row126521548145814"><tdclass="cellrowborder"valign="top"width="42.980000000000004%"headers="mcps1.2.3.1.1 "><pid="p2652134812586"><aname="p2652134812586"></a><aname="p2652134812586"></a>on('error', function callback)</p>
</td>
<tdclass="cellrowborder"valign="top"width="57.02%"headers="mcps1.2.3.1.2 "><pid="p116527483584"><aname="p116527483584"></a><aname="p116527483584"></a>Subscribes to the audio recording error event.</p>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p17715173772211"><aname="p17715173772211"></a><aname="p17715173772211"></a>Audio encoding format. The default value is <strongid="b14896101415519"><aname="b14896101415519"></a><aname="b14896101415519"></a>AAC_LC</strong>.</p>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p20715537112211"><aname="p20715537112211"></a><aname="p20715537112211"></a>Bit rate for audio encoding. The default value is <strongid="b1433419231554"><aname="b1433419231554"></a><aname="b1433419231554"></a>48000</strong>.</p>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p15991111611271"><aname="p15991111611271"></a><aname="p15991111611271"></a>Sampling rate for audio encoding. The default value is <strongid="b11540104210555"><aname="b11540104210555"></a><aname="b11540104210555"></a>48000</strong>.</p>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p7537102393512"><aname="p7537102393512"></a><aname="p7537102393512"></a>Number of audio channels. The default value is <strongid="b1437255075515"><aname="b1437255075515"></a><aname="b1437255075515"></a>2</strong>.</p>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p6719182923513"><aname="p6719182923513"></a><aname="p6719182923513"></a>Audio output format. The default value is <strongid="b1290655565511"><aname="b1290655565511"></a><aname="b1290655565511"></a>MPEG_4</strong>.</p>
<tdclass="cellrowborder"valign="top"width="57.709999999999994%"headers="mcps1.2.3.1.2 "><pid="p14918142711351"><aname="p14918142711351"></a><aname="p14918142711351"></a>Audio output path, which starts with <strongid="b148591619165613"><aname="b148591619165613"></a><aname="b148591619165613"></a>file://path</strong> or <strongid="b421882325615"><aname="b421882325615"></a><aname="b421882325615"></a>file://fd</strong>.</p>
</td>
</tr>
</tbody>
</table>
## How to Develop
**Table 4** AudioEncoder
For details about the APIs used for audio recording, see [js-apis-media.md](../reference/apis/js-apis-media.md).
The full audio recording process includes creating an instance, setting recording parameters, starting, pausing, resuming, and stopping recording, and releasing resources.
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();
```js
functionSetCallBack(audioRecorder){
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
// The recording page is ready. You can click the Record button to start recording.
});
recorder.on('start', () => {
console.info('setCallback start() case callback is called');
setTimeout(function(){
recorder.pause();
}, 10000); // Pause recording 10 seconds after the recording starts.
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
// The Record button is changed to the pausable state.
});
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.
audioRecorder.on('pause',()=>{// Set the 'pause' event callback.
console.log('audio recorder pause success');
// The Record button is changed to the recordable state.
});
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.
audioRecorder.on('resume',()=>{// Set the 'resume' event callback.
console.log('audio recorder resume success');
// The Record button is changed to the pausable state.
});
recorder.on('stop', () => {
console.info('setCallback stop() case callback is called');
recorder.release();
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
});
recorder.on('release', () => {
console.info('setCallback release() case callback is called');
audioRecorder.on('release',()=>{// Set the 'release' event callback.
console.log('audio recorder release success');
});
recorder.on('error', (err) => {
console.info(`case error called,errCode is ${err.code}`);
console.info(`case error called,errMessage is ${err.message}`);
recorder.reset();
audioRecorder.on('reset',()=>{// Set the 'reset' event callback.
console.log('audio recorder reset success');
// You need to reset the recording parameters for another recording.
});
```
audioRecorder.on('error',(error)=>{// Set the 'error' event callback.
console.info(`audio error called, errName is ${error.name}`);
console.info(`audio error called, errCode is ${error.code}`);
console.info(`audio error called, errMessage is ${error.message}`);
});
}
// 1. Create an AudioRecorder instance.
letaudioRecorder=media.createAudioRecorder();
// 2. Set the callbacks.
SetCallBack(audioRecorder);
// 3. Set the recording parameters.
letaudioRecorderConfig={
audioEncoder:media.AudioEncoder.AAC_LC,
audioEncodeBitRate:22050,
audioSampleRate:22050,
numberOfChannels:2,
format:media.AudioOutputFormat.AAC_ADTS,
uri:'file:///data/accounts/account_0/appdata/appdata/recorder/test.m4a',// The file must be created by the caller and granted with proper permissions.
location:{latitude:30,longitude:130},
}
audioRecorder.prepare(audioRecorderConfig);
// 4. Start recording.
audioRecorder.start();// The start method can be called to trigger the 'start' event callback only after the 'prepare' event callback is complete.
// 5. Pause recording.
audioRecorder.pause();// The pause method can be called to trigger the 'pause' event callback only after the 'start' event callback is complete.
// 6. Resume recording.
audioRecorder.resume();// The resume method can be called to trigger the 'resume' event callback only after the 'pause' event callback is complete.
// 7. Stop recording.
audioRecorder.stop();// The stop method can be called to trigger the 'stop' event callback only after the 'start' or 'resume' event callback is complete.
// 8. Reset recording.
audioRecorder.reset();// The prepare method can be called for another recording only after the 'reset' event callback is complete.
// 9. Release resources.
audioRecorder.release();// The AudioRecorder resource is destroyed.
audioRecorder=undefined;
```
### Normal Recording Scenario
Unlike the full-process scenario, the normal recording scenario does not include the process of pausing and resuming recording.
```js
functionSetCallBack(audioPlayer){
audioRecorder.on('prepare',()=>{// Set the 'prepare' event callback.
console.log('prepare success');
// The recording page is ready. You can click the Record button to start recording.
});
audioRecorder.on('start',()=>{// Set the 'start' event callback.
console.log('audio recorder start success');
// The Record button is changed to the pausable state.
});
audioRecorder.on('stop',()=>{// Set the 'stop' event callback.
console.log('audio recorder stop success');
});
audioRecorder.on('release',()=>{// Set the 'release' event callback.
console.log('audio recorder release success');
});
}
// 1. Create an AudioRecorder instance.
letaudioRecorder=media.createAudioRecorder();
// 2. Set the callbacks.
SetCallBack(audioRecorder);
// 3. Set the recording parameters.
letaudioRecorderConfig={
audioEncoder:media.AudioEncoder.AAC_LC,
audioEncodeBitRate:22050,
audioSampleRate:22050,
numberOfChannels:2,
format:media.AudioOutputFormat.AAC_ADTS,
uri:'file:///data/accounts/account_0/appdata/appdata/recorder/test.m4a',// The file must be created by the caller and granted with proper permissions.
location:{latitude:30,longitude:130},
}
audioRecorder.prepare(audioRecorderConfig)
// 4. Start recording.
audioRecorder.start();// The start method can be called to trigger the 'start' event callback only after the 'prepare' event callback is complete.
// 5. Stop recording.
audioRecorder.stop();// The stop method can be called to trigger the 'stop' event callback only after the 'start' or 'resume' event callback is complete.
// 6. Release resources.
audioRecorder.release();// The AudioRecorder resource is destroyed.
During video recording, audio and video signals are captured, encoded, and saved to files. You can specify parameters such as the encoding format, encapsulation format, and file path for video recording.
For details about the APIs used for video recording, see [js-apis-media.md](../reference/apis/js-apis-media.md).
### Full-Process Scenario
The full video recording process includes creating an instance, setting recording parameters, recording video, pausing, resuming, and stopping recording, and releasing resources.
```js
letvideoProfile={
audioBitrate:48000,
audioChannels:2,
audioCodec:'audio/mp4a-latm',
audioSampleRate:48000,
fileFormat:'mp4',
videoBitrate:48000,
videoCodec:'video/mp4v-es',
videoFrameWidth:640,
videoFrameHeight:480,
videoFrameRate:30
}
letvideoConfig={
audioSourceType:1,
videoSourceType:0,
profile:videoProfile,
url:'file:///data/media/01.mp4',
orientationHint:0,
location:{latitude:30,longitude:130},
}
// Error callback triggered in the case of an error
functionfailureCallback(error){
console.info('error happened, error name is '+error.name);
console.info('error happened, error code is '+error.code);
console.info('error happened, error message is '+error.message);
}
// Error callback triggered in the case of an exception
functioncatchCallback(error){
console.info('catch error happened, error name is '+error.name);
console.info('catch error happened, error code is '+error.code);
console.info('catch error happened, error message is '+error.message);
}
letvideoRecorder=null;// videoRecorder is an empty object and assigned with a value after createVideoRecorder is successfully called.
letsurfaceID=null;// Used to save the surface ID returned by getInputSurface.
// Video recording depends on camera-related interfaces. The following operations can be performed only after the video output start interface is invoked.
// Start video recording.
awaitvideoRecorder.start().then(()=>{
console.info('start success');
},failureCallback).catch(catchCallback);
// Pause video playback before the video output stop interface is invoked.
awaitvideoRecorder.pause().then(()=>{
console.info('pause success');
},failureCallback).catch(catchCallback);
// Resume video playback after the video output start interface is invoked.
awaitvideoRecorder.resume().then(()=>{
console.info('resume success');
},failureCallback).catch(catchCallback);
// Stop video recording after the video output stop interface is invoked.
awaitvideoRecorder.stop().then(()=>{
console.info('stop success');
},failureCallback).catch(catchCallback);
// Reset the recording configuration.
awaitvideoRecorder.reset().then(()=>{
console.info('reset success');
},failureCallback).catch(catchCallback);
// Release the video recording resources and camera object resources.