申请语音权限
ohos.permission.MICROPHONE
初始化引擎
this.asrEngine = await speechRecognizer.createEngine({
language: 'zh-CN', // 中文
online: 1 // 离线
})
设置回调,用来接收语音识别相关的回调信息
export enum VoiceState {
DEFAULT, // 默认
VOICING, // 录音中
VOICEOVER, // 录音结束
}
const _this = this
this.asrEngine.setListener({
// 开始识别成功回调
onStart(sessionId: string, eventMessage: string) {
console.info(`onStart, sessionId: ${sessionId} eventMessage: ${eventMessage}`);
},
// 事件回调
onEvent(sessionId: string, eventCode: number, eventMessage: string) {
console.info(`onEvent, sessionId: ${sessionId} eventCode: ${eventCode} eventMessage: ${eventMessage}`);
},
// 识别结果回调,包括中间结果和最终结果
onResult(sessionId: string, result: speechRecognizer.SpeechRecognitionResult) {
_this.keyword = result.result
_this.onChange(result.result)
console.info(`onResult, sessionId: ${sessionId} sessionId: ${JSON.stringify(result)}`);
},
// 识别完成回调
onComplete(sessionId: string, eventMessage: string) {
_this.onComplete(_this.keyword)
_this.keyword = ''
_this.voiceState = VoiceState.DEFAULT
console.info(`onComplete, sessionId: ${sessionId} eventMessage: ${eventMessage}`);
},
// 错误回调
onError(sessionId: string, errorCode: number, errorMessage: string) {
console.error(`onError, sessionId: ${sessionId} errorCode: ${errorCode} errorMessage: ${errorMessage}`);
}
})
设置开始识别的相关参数,调用startListening方法,开始合成
this.asrEngine?.startListening({
sessionId: this.asrEngineId, // 会话ID
audioInfo: {
audioType: 'pcm', // 音频类型
sampleRate: 16000, // 音频的采样率
soundChannel: 1, // 音频返回的通道数信息
sampleBit: 16 // 音频返回的采样位数
}
})
录制音频
1.配置音频采集参数并创建AudioCapturer实例
// 表示音频流信息
const audioStreamInfo: audio.AudioStreamInfo = {
samplingRate: audio.AudioSamplingRate.SAMPLE_RATE_16000, // 音频文件的采样率
channels: audio.AudioChannel.CHANNEL_1, // 音频文件的通道数
sampleFormat: audio.AudioSampleFormat.SAMPLE_FORMAT_S16LE, // 音频采样格式
encodingType: audio.AudioEncodingType.ENCODING_TYPE_RAW // 音频编码格式
}
// 表示采集器信息
const audioCapturerInfo: audio.AudioCapturerInfo = {
source: audio.SourceType.SOURCE_TYPE_MIC, // 音频类型
capturerFlags: 0 // 音频采集器标志
}
const audioCapturerOptions: audio.AudioCapturerOptions = {
streamInfo: audioStreamInfo,
capturerInfo: audioCapturerInfo
}
this.audioCapturer = await audio.createAudioCapturer(audioCapturerOptions)
- 调用on(‘readData’)方法,订阅监听音频数据读入回调
this.audioCapturer.on('readData', (buffer) => {
this.asrEngine?.writeAudio(this.asrEngineId, new Uint8Array(buffer))
})
- 调用start()方法进入running状态,开始录制音频
await this.audioCapturer.start()
this.voiceState = VoiceState.VOICING
取消录制
async closeRecord() {
this.audioCapturer?.stop() // 调用stop()方法停止录制
this.audioCapturer?.release() 调用release()方法销毁实例,释放资源
// 结束识别,调用 finish 方法
this.asrEngine?.finish(this.asrEngineId)
// 取消识别,调用 cancel 方法
this.asrEngine?.cancel(this.asrEngineId)
// 释放语音识别引擎资源,调用shutdown方法
this.asrEngine?.shutdown()
if (this.keyword) {
this.voiceState = VoiceState.DEFAULT
this.keyword = ''
} else {
this.voiceState = VoiceState.VOICEOVER
}
}
通过长按属性执行开始录音和取消录音
.gesture(
// 长按手势
LongPressGesture()
.onAction(() => {
// 长按开始-开始录音
this.startRecord()
})
.onActionEnd(() => {
// 长按结束-结束录音
this.closeRecord()
})
.onActionCancel(() => {
// 长按取消-结束录音
this.closeRecord()
})
)
写在最后
●如果你觉得这篇内容对你还蛮有帮助,我想邀请你帮我三个小忙:
●点赞,转发,有你们的 『点赞和评论』,才是我创造的动力。
●关注小编,同时可以期待后续文章ing🚀,不定期分享原创知识。
●更多鸿蒙最新技术知识点,请移步前往小编:https://gitee.com/