has.stopRecord(Object object)
停止录音。
起始版本: 1.0.14
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 70003901 | error. |
| 70004001 | stopRecord error. |
示例:
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(Object object)
开始录音。当主动调用has.stopRecord,或者录音超过1分钟时自动结束录音。
当用户离开元服务时,此接口无法调用。
起始版本: 1.0.14
需要权限:
在has.authorize中申请 scope.record 授权。
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
success返回值:
Object类型,包括以下字段。
| 参数 | 类型 | 描述 |
|---|---|---|
| tempFilePath | string | 录音文件的临时路径(本地路径)。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 70003901 | startRecord error. |
示例:
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(): RecorderManager
获取全局唯一的录音管理器RecorderManager。
起始版本: 1.0.14
返回值:
返回RecorderManager对象。
示例:
const recorderManager = has.getRecorderManager();
RecorderManager.onError(function callback)
监听录音错误事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 录音错误事件的监听函数。 |
callback参数:
类型为Object对象,包括:
| 参数 | 类型 | 描述 |
|---|---|---|
| errMsg | string | 错误信息。 |
示例:
const recordManager = has.getRecorderManager();
recordManager.onError((res) => {
console.error('Recording onError res', res);
}); RecorderManager.offError(function callback)
移除监听录音错误事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | RecorderManager.onError传入的监听函数,不传此参数则移除所有监听函数。 |
示例:
const recordManager = has.getRecorderManager();
const callback = (res) => {
console.info('Recording onError res', res);
}
recordManager.onError(callback);
recordManager.offError(callback); RecorderManager.onPause(function callback)
监听录音暂停事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 录音暂停事件的监听函数。 |
示例:
const recordManager = has.getRecorderManager();
recordManager.onPause((res) => {
console.info('Recording onPause res', res);
}); RecorderManager.offPause(function callback)
移除录音暂停事件的监听函数。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | RecorderManager.onPause传入的监听函数,不传此参数则移除所有监听函数。 |
示例:
const recordManager = has.getRecorderManager();
const callback = (res) => {
console.info('Recording onPause res', res);
}
recordManager.onPause(callback);
recordManager.offPause(callback); RecorderManager.onResume(function callback)
监听录音继续事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 录音继续事件的监听函数。 |
示例:
const recordManager = has.getRecorderManager();
recordManager.onResume((res) => {
console.info('Recording onResume res', res);
}); RecorderManager.offResume(function callback)
移除监听录音继续事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | RecorderManager.onResume传入的监听函数,不传此参数则移除所有监听函数。 |
示例:
const recordManager = has.getRecorderManager();
const callback = (res) => {
console.info('Recording onResume res', res);
}
recordManager.onResume(callback);
recordManager.offResume(callback); RecorderManager.onStart(function callback)
监听录音开始事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 录音开始事件的监听函数。 |
示例:
const recordManager = has.getRecorderManager();
recordManager.onStart((res) => {
console.info('Recording onStart res', res);
}); RecorderManager.offStart(function callback)
移除监听录音开始事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | RecorderManager.onStart传入的监听函数,不传此参数则移除所有监听函数。 |
示例:
const recordManager = has.getRecorderManager();
const callback = (res) => {
console.info('Recording onStart res', res);
}
recordManager.onStart(callback);
recordManager.offStart(callback); RecorderManager.onStop(function callback)
监听录音结束事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 是 | 录音结束事件的监听函数。 |
callback参数:
类型为Object对象,包括:
| 参数 | 类型 | 描述 |
|---|---|---|
| tempFilePath | string | 录音文件的临时路径(本地路径)。 |
| duration | number | 录音总时长,单位:ms。 |
| fileSize | number | 录音文件大小,单位:byte。 |
示例:
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);
}); RecorderManager.offStop(function callback)
移除监听录音结束事件。
起始版本: 1.0.14
参数:
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| callback | function | 否 | RecorderManager.onStop传入的监听函数,不传此参数则移除所有监听函数。 |
示例:
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); RecorderManager.start(Object object): void
开始录音。
起始版本: 1.0.14
需要权限:
在has.authorize中申请 scope.record 授权。
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 默认值 | 必填 | 描述 |
|---|---|---|---|---|
| duration | number | 60000 | 否 | 录音的时长,单位ms,最大值600000(10分钟)。 |
| sampleRate | number | 8000 | 否 | 采样率,有效值见下表 采样率有效值。 |
| numberOfChannels | number | 2 | 否 | 录音通道数。有效值: 1:1个通道 2:2个通道。 |
| encodeBitRate | number | 48000 | 否 | 编码码率,有效值见下表。 |
| format | string | aac | 否 | 音频格式。有效值: - mp3 - aac - wav。 |
| audioSource | string | auto | 否 | 指定录音的音频输入源。有效值如下: - auto:自动设置,默认使用手机麦克风。 - mic:麦克风。 - camcorder:相机录像的音频源。 - voiceCommunication:语音通话场景的音频源。 - voiceRecognition:语音识别场景的音频源。 |
表1 采样率有效值
| 编码格式 | 封装格式 | 采样率 | 比特率 | 声道数 |
|---|---|---|---|---|
| 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] 采样率使用16000以下时,对应比特率范围为[8000 - 64000]。 采样率使用16000~32000时对应的比特率范围为[8000 - 160000]。 采样率使用32000以上时对应的比特率范围为[32000 - 320000]。 | [1-2] |
| AUDIO_G711MU | WAV | [8000] | [64000] | [1] |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | authorize fail. |
| 103 | 1. start -> AVRecorder start has no permission. 2. has no permission. |
| 70003902 | RecorderManager.start. |
示例:
const recordManager = has.getRecorderManager();
this.recordManager.start({
duration: 60000,
sampleRate: 8000,
numberOfChannels: 2,
encodeBitRate: 48000,
format: 'aac',
}); RecorderManager.pause()
暂停录音。
起始版本: 1.0.14
需要权限:
在has.authorize中申请 scope.record 授权。
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | authorize fail. |
| 103 | has no permission. |
示例:
const recordManager = has.getRecorderManager();
recordManager.start({
duration: 60000,
sampleRate: 8000,
numberOfChannels: 2,
encodeBitRate: 48000,
format: 'aac',
});
recordManager.pause(); RecorderManager.resume()
继续录音。
起始版本: 1.0.14
需要权限:
在has.authorize中申请 scope.record 授权。
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | authorize fail. |
| 103 | has no permission. |
示例:
const recordManager = has.getRecorderManager();
recordManager.start({
duration: 60000,
sampleRate: 8000,
numberOfChannels: 2,
encodeBitRate: 48000,
format: 'aac',
});
recordManager.pause();
recordManager.resume(); RecorderManager.stop()
停止录音。
起始版本: 1.0.14
需要权限:
在has.authorize中申请 scope.record 授权。
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 101 | 1. authorize fail. 2. AVRecorder is not initialized. |
| 103 | has no permission. |
示例:
const recordManager = has.getRecorderManager();
recordManager.start({
duration: 60000,
sampleRate: 8000,
numberOfChannels: 2,
encodeBitRate: 48000,
format: 'aac',
});
recordManager.stop();