Deprecated Interface (AudioRecorder, deprecated)
NOTE
This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder instead.
AudioRecorder is a class for audio recording management. It provides APIs to record audio. Before calling any API in AudioRecorder, you must use createAudioRecorder() to create an AudioRecorder instance.
Modules to Import
import { media } from '@kit.MediaKit';
prepare(deprecated)
prepare(config: AudioRecorderConfig): void
Prepares for recording.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.prepare instead.
Required permissions: ohos.permission.MICROPHONE
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| config | AudioRecorderConfig | Yes | Audio recording parameters, including the audio output URI, encoding format, sample rate, audio channel count, and output format. |
Error codes
For details about the error codes, see Media Error Codes.
| ID | Error Message |
|---|---|
| 201 | permission denied |
Example
let audioRecorderConfig: media.AudioRecorderConfig = {
audioEncoder : media.AudioEncoder.AAC_LC,
audioEncodeBitRate : 64000,
audioSampleRate : 44100,
numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS,
uri : 'fd://1', // The file must be created by the caller and granted with proper permissions.
location : { latitude : 30, longitude : 130},
};
audioRecorder.on('prepare', () => { // Set the 'prepare' event callback.
console.info('prepare called');
});
audioRecorder.prepare(audioRecorderConfig);
start(deprecated)
start(): void
Starts audio recording. This API can be called only after the 'prepare' event is triggered.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.start instead.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Example
audioRecorder.on('start', () => { // Set the 'start' event callback.
console.info('audio recorder start called');
});
audioRecorder.start();
pause(deprecated)
pause():void
Pauses audio recording. This API can be called only after the 'start' event is triggered.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.pause instead.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Example
audioRecorder.on('pause', () => { // Set the 'pause' event callback.
console.info('audio recorder pause called');
});
audioRecorder.pause();
resume(deprecated)
resume():void
Resumes audio recording. This API can be called only after the 'pause' event is triggered.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.resume instead.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Example
audioRecorder.on('resume', () => { // Set the 'resume' event callback.
console.info('audio recorder resume called');
});
audioRecorder.resume();
stop(deprecated)
stop(): void
Stops audio recording.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.stop instead.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Example
audioRecorder.on('stop', () => { // Set the 'stop' event callback.
console.info('audio recorder stop called');
});
audioRecorder.stop();
release(deprecated)
release(): void
Releases the audio recording resources.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.release instead.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Example
audioRecorder.on('release', () => { // Set the 'release' event callback.
console.info('audio recorder release called');
});
audioRecorder.release();
audioRecorder = undefined;
reset(deprecated)
reset(): void
Resets audio recording.
Before resetting audio recording, you must call stop() to stop recording. After audio recording is reset, you must call prepare() to set the recording configurations for another recording.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.reset instead.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Example
audioRecorder.on('reset', () => { // Set the 'reset' event callback.
console.info('audio recorder reset called');
});
audioRecorder.reset();
on('prepare'|'start'|'pause'|'resume'|'stop'|'release'|'reset')(deprecated)
on(type: 'prepare'|'start'|'pause'|'resume'|'stop'|'release'|'reset', callback: () => void): void
Subscribes to the audio recording events.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.on('stateChange') instead.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. The following events are supported: 'prepare'|'start'| 'pause' |'resume' |'stop'|'release'|'reset' - 'prepare': triggered when the prepare() API is called and the audio recording parameters are set. - 'start': triggered when the start() API is called and audio recording starts. - 'pause': triggered when the pause() API is called and audio recording is paused. - 'resume': triggered when the resume() API is called and audio recording is resumed. - 'stop': triggered when the stop() API is called and audio recording stops. - 'release': triggered when the release() API is called and the recording resources are released. - 'reset': triggered when the reset() API is called and audio recording is reset. |
| callback | ()=>void | Yes | Callback invoked when the event is triggered. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
let audioRecorder: media.AudioRecorder = media.createAudioRecorder(); // Create an AudioRecorder instance.
let audioRecorderConfig: media.AudioRecorderConfig = {
audioEncoder : media.AudioEncoder.AAC_LC,
audioEncodeBitRate : 64000,
audioSampleRate : 44100,
numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS,
uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions.
location : { latitude : 30, longitude : 130}
};
audioRecorder.on('error', (error: BusinessError) => { // Set the 'error' event callback.
console.error(`audio error called, error: ${error}`);
});
audioRecorder.on('prepare', () => { // Set the 'prepare' event callback.
console.info('prepare called');
audioRecorder.start(); // // Start recording and trigger the 'start' event callback.
});
audioRecorder.on('start', () => { // Set the 'start' event callback.
console.info('audio recorder start called');
});
audioRecorder.on('pause', () => { // Set the 'pause' event callback.
console.info('audio recorder pause called');
});
audioRecorder.on('resume', () => { // Set the 'resume' event callback.
console.info('audio recorder resume called');
});
audioRecorder.on('stop', () => { // Set the 'stop' event callback.
console.info('audio recorder stop called');
});
audioRecorder.on('release', () => { // Set the 'release' event callback.
console.info('audio recorder release called');
});
audioRecorder.on('reset', () => { // Set the 'reset' event callback.
console.info('audio recorder reset called');
});
audioRecorder.prepare(audioRecorderConfig) // Set recording parameters and trigger the 'prepare' event callback.
on('error')(deprecated)
on(type: 'error', callback: ErrorCallback): void
Subscribes to audio recording error events. After an error event is reported, you must handle the event and exit the recording.
NOTE This API is supported since API version 6 and deprecated since API version 9. You are advised to use AVRecorder.on('error') instead.
System capability: SystemCapability.Multimedia.Media.AudioRecorder
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type, which is 'error' in this case. This event is triggered when an error occurs during audio recording. |
| callback | ErrorCallback | Yes | Callback invoked when the event is triggered. |
Example
import { BusinessError } from '@kit.BasicServicesKit';
let audioRecorderConfig: media.AudioRecorderConfig = {
audioEncoder : media.AudioEncoder.AAC_LC,
audioEncodeBitRate : 22050,
audioSampleRate : 22050,
numberOfChannels : 2,
format : media.AudioOutputFormat.AAC_ADTS,
uri : 'fd://xx', // The file must be created by the caller and granted with proper permissions.
location : { latitude : 30, longitude : 130}
};
audioRecorder.on('error', (error: BusinessError) => { // Set the 'error' event callback.
console.error(`audio error called, error: ${error}`);
});
audioRecorder.prepare(audioRecorderConfig); // Do not set any parameter in prepare and trigger the 'error' event callback.
你可能感兴趣的鸿蒙文章
openharmony 鸿蒙 capi-avrecorder-oh-avrecorder-range
openharmony 鸿蒙 errorcode-media
openharmony 鸿蒙 capi-avplayer-base-h
openharmony 鸿蒙 capi-avimage-generator-h
openharmony 鸿蒙 capi-avscreencapture-oh-rect
openharmony 鸿蒙 capi-videoprocessing-videoprocessing-callback
openharmony 鸿蒙 capi-avsinkbase
openharmony 鸿蒙 capi-avmetadataextractor
openharmony 鸿蒙 capi-avscreencapture-oh-multidisplaycapability