step1: 用手机录制一段中文语音(调用avPlayer能如预期播放)
private avProfile: media.AVRecorderProfile = {
audioBitrate: 100000, // 音频比特率
audioChannels: 2, // 音频声道数
audioCodec: media.CodecMimeType.AUDIO_AAC, // 音频编码格式,当前只支持aac
audioSampleRate: 48000, // 音频采样率
fileFormat: media.ContainerFormatType.CFT_MPEG_4A, // 封装格式,当前只支持m4a
};
参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/using-avrecorder-for-recording-V5
step2 用speechRecognizer读取并识别这段语音,却识别不到不效信息。
const audioRecorder = new AudioRecorderDemo();
await audioRecorder.startRecordingProcess(ctx);
await this.sleep(3000);
const audioFilePath = await audioRecorder.stopRecordingProcess();
let file = fileIo.openSync(audioFilePath, fileIo.OpenMode.READ_WRITE);
try {
let buf: ArrayBuffer = new ArrayBuffer(1280);
let offset: number = 0;
while (1280 == fileIo.readSync(file.fd, buf, {
offset: offset
})) {
let uint8Array: Uint8Array = new Uint8Array(buf);
asrEngine.writeAudio("123456", uint8Array);
await this.countDownLatch(1);
offset = offset + 1280;
}
}
speechRecognizer.RecognitionListener的onResult
{"isFinal":true,"isLast":true,"result":""}
语音识别目前只支持pcm格式文件,m4a格式暂时不支持,而AVRecorder的格式是m4a格式。
AudioCapturer是音频采集器,用于录制PCM(Pulse Code Modulation)音频数据,建议使用AudioCapturer。
参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/using-audiocapturer-for-recording-V5