目录

Recording

has.stopRecord

has.stopRecord(Object object)

Stops recording.

Since: 1.0.14

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Error code information

Error Code Error Message
70003901 error.
70004001 stopRecord error.

Example:

has.stopRecord({
  success: (res) => {
    console.info('stopRecord success', res);
  },
  fail: (err) => {
    console.error('stopRecord fail', err);
  },
  complete: (res) => {
    console.info('stopRecord complete', res);
  }
});

has.startRecord

has.startRecord(Object object)

Starts recording. When has.stopRecord is called or the recording duration exceeds 1 minute, the recording automatically ends.

This API cannot be called when a user exits the atomic service.

Since: 1.0.14

Required permissions:

  1. ohos.permission.MICROPHONE, which needs to be declared in module.json5.

  2. scope.record permission, which is applied for in has.authorize.

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

Return value of success

An object containing the following fields.

Parameter Type Description
tempFilePath string Temporary path (local path) of the recording file.

Error code information

Error Code Error Message
70003901 startRecord error.

Example:

has.startRecord({
  success: (res) => {
    console.info('startRecord success', res);
  },
  fail: (err) => {
    console.error('startRecord fail', err);
  },
  complete: (res) => {
    console.info('startRecord complete', res);
  }
});

has.getRecorderManager

has.getRecorderManager(): RecorderManager

Obtains the globally unique recording manager (RecorderManager).

Since: 1.0.14

Returns

RecorderManager object.

Example:

const recorderManager = has.getRecorderManager();

RecorderManager

Indicates the globally unique recording manager.

Since: 1.0.14

[h2]RecorderManager.onError

RecorderManager.onError(function callback)

Listens for recording error events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function Yes Listens for recording error events.

callback parameters:

An object containing the following fields.

Parameter Type Description
errMsg string Error message.

Example:

const recordManager = has.getRecorderManager();
recordManager.onError((res) => {
  console.error('Recording onError res', res);
});

[h2]RecorderManager.offError

RecorderManager.offError(function callback)

Removes listening for recording error events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by RecorderManager.onError. If this parameter is not passed, all listener functions are removed.

Example:

const recordManager = has.getRecorderManager();
const callback = (res) => {
  console.info('Recording onError res', res);
}
recordManager.onError(callback);
recordManager.offError(callback);

[h2]RecorderManager.onPause

RecorderManager.onPause(function callback)

Listens for recording pause events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function Yes Listens for recording pause events.

Example:

const recordManager = has.getRecorderManager();
recordManager.onPause((res) => {
  console.info('Recording onPause res', res);
});

[h2]RecorderManager.offPause

RecorderManager.offPause(function callback)

Removes listening for recording pause events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by RecorderManager.onPause. If this parameter is not passed, all listener functions are removed.

Example:

const recordManager = has.getRecorderManager();
const callback = (res) => {
  console.info('Recording onPause res', res);
}
recordManager.onPause(callback);
recordManager.offPause(callback);

[h2]RecorderManager.onResume

RecorderManager.onResume(function callback)

Listens for recording resumption events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function Yes Listens for recording resumption events.

Example:

const recordManager = has.getRecorderManager();
recordManager.onResume((res) => {
  console.info('Recording onResume res', res);
});

[h2]RecorderManager.offResume

RecorderManager.offResume(function callback)

Removes listening for recording resumption events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by RecorderManager.onResume. If this parameter is not passed, all listener functions are removed.

Example:

const recordManager = has.getRecorderManager();
const callback = (res) => {
  console.info('Recording onResume res', res);
}
recordManager.onResume(callback);
recordManager.offResume(callback);

[h2]RecorderManager.onStart

RecorderManager.onStart(function callback)

Listens for recording start events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function Yes Listens for recording start events.

Example:

const recordManager = has.getRecorderManager();
recordManager.onStart((res) => {
  console.info('Recording onStart res', res);
});

[h2]RecorderManager.offStart

RecorderManager.offStart(function callback)

Removes listening for recording start events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by RecorderManager.onStart. If this parameter is not passed, all listener functions are removed.

Example:

const recordManager = has.getRecorderManager();
const callback = (res) => {
  console.info('Recording onStart res', res);
}
recordManager.onStart(callback);
recordManager.offStart(callback);

[h2]RecorderManager.onStop

RecorderManager.onStop(function callback)

Listens for recording stop events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function Yes Listens for recording stop events.

callback parameters:

An object containing the following fields.

Parameter Type Description
tempFilePath string Temporary path (local path) of the recording file.
duration number Total recording duration, in milliseconds.
fileSize number Size of the recording file, in bytes.

Example:

const recordManager = has.getRecorderManager();
recordManager.onStop((res) => {
  console.info('Recording onStop res', res);
  console.info(res.tempFilePath);
  console.info(res.duration);
  console.info(res.fileSize);
});

[h2]RecorderManager.offStop

RecorderManager.offStop(function callback)

Removes listening for recording stop events.

Since: 1.0.14

Parameters

Parameter Type Mandatory Description
callback function No Listener function passed by RecorderManager.onStop. If this parameter is not passed, all listener functions are removed.

Example:

const recordManager = has.getRecorderManager();
const callback = (res) => {
  console.info('Recording onStop res', res);
  console.info(res.tempFilePath);
  console.info(res.duration);
  console.info(res.fileSize);
};
recordManager.onStop(callback);
recordManager.offStop(callback);

[h2]RecorderManager.start

RecorderManager.start(Object object): void

Starts recording.

Since: 1.0.14

Required permissions:

  1. ohos.permission.MICROPHONE, which needs to be declared in module.json5.

  2. scope.record permission, which is applied for in has.authorize.

Parameters

The input parameter is an object containing the following fields.

Parameter Type Default Value Mandatory Description
duration number 60000 No Recording duration, in milliseconds. The maximum value is 600000 (10 minutes).
sampleRate number 8000 No Sampling rate. For details about the valid values, see the table below.
numberOfChannels number 2 No

Number of recording channels. Valid values:

1: one channel

2: two channels

encodeBitRate number 48000 No Encoding bit rate. For details about the valid values, see the following below.
format string aac No

Audio format. Valid values:

- mp3

- aac

- wav

audioSource string auto No

Audio input source of the recording. Valid values:

- auto: The audio input source is automatically set. By default, the smartphone microphone is used.

- mic: microphone.

- camcorder: audio source in camera recording scenarios.

- voiceCommunication: audio source in voice communication scenarios.

- voiceRecognition: audio source in speech recognition scenarios.

Table 1 Valid values of the sampling rate

Encoding Format Container Format Sampling Rate Bit Rate Number of Audio Channels
AUDIO_AAC M4A [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000, 64000, 88200, 96000] [32000-500000] [1-2]
AUDIO_MP3 MP3 [8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000]

[8000, 16000, 32000, 40000, 48000, 56000, 64000, 80000, 96000, 112000, 128000, 160000, 192000, 224000, 256000, 320000]

When the sampling rate is lower than 16000, the bit rate range is [8000 - 64000].

When the sampling rate ranges from 16000 to 32000, the bit rate range is [8000 - 160000].

When the sampling rate is greater than 32000, the bit rate range is [32000 - 320000].

[1-2]
AUDIO_G711MU WAV [8000] [64000] [1]

Error code information

Error Code Error Message
101 authorize fail.
103

1. start -> AVRecorder start has no permission.

2. has no permission.

70003902 RecorderManager.start.

Example:

const recordManager = has.getRecorderManager();
this.recordManager.start({
  duration: 60000,
  sampleRate: 8000,
  numberOfChannels: 2,
  encodeBitRate: 48000,
  format: 'aac',
});

[h2]RecorderManager.pause

RecorderManager.pause()

Pauses recording.

Since: 1.0.14

Required permissions:

  1. ohos.permission.MICROPHONE, which needs to be declared in module.json5.

  2. scope.record permission, which is applied for in has.authorize.

Error code information

Error Code Error Message
101 authorize fail.
103 has no permission.

Example:

const recordManager = has.getRecorderManager();
recordManager.start({
  duration: 60000,
  sampleRate: 8000,
  numberOfChannels: 2,
  encodeBitRate: 48000,
  format: 'aac',
});
recordManager.pause();

[h2]RecorderManager.resume

RecorderManager.resume()

Resumes recording.

Since: 1.0.14

Required permissions:

  1. ohos.permission.MICROPHONE, which needs to be declared in module.json5.

  2. scope.record permission, which is applied for in has.authorize.

Error code information

Error Code Error Message
101 authorize fail.
103 has no permission.

Example:

const recordManager = has.getRecorderManager();
recordManager.start({
  duration: 60000,
  sampleRate: 8000,
  numberOfChannels: 2,
  encodeBitRate: 48000,
  format: 'aac',
});
recordManager.pause();
recordManager.resume();

[h2]RecorderManager.stop

RecorderManager.stop()

Stops recording.

Since: 1.0.14

Required permissions:

  1. ohos.permission.MICROPHONE, which needs to be declared in module.json5.

  2. scope.record permission, which is applied for in has.authorize.

Error code information

Error Code Error Message
101

1. authorize fail.

2. AVRecorder is not initialized.

103 has no permission.

Example:

const recordManager = has.getRecorderManager();
recordManager.start({
  duration: 60000,
  sampleRate: 8000,
  numberOfChannels: 2,
  encodeBitRate: 48000,
  format: 'aac',
});
recordManager.stop();