admin管理员组文章数量:1444823
HarmonyOS NEXT 语音搜索场景学习和总结
大部分app的搜索页面都已经支持语音搜索,以下简单介绍以下HarmonyOS在语音搜索上的使用和总结
语音搜索需要在module.json5中配置麦克风权限:ohos.permission.HICROPHONE
在调起麦克风之前要对该权限进行检查和申请;具体检查和申请的步骤请查看博主相关文章
下面场景介绍PCM文件每帧容重(单位:字节数)计算公式:采样频率*采样位数*声道*时间/8,每0.1秒发一个包,将音频数据base64编码后,调用服务端接口进行数据解码,避免敏感词的出现
代码语言:javascript代码运行次数:0运行复制GRPermissionsUtils.checkPermissions(permission).then((grantStatus: abilityAccessCtrl.GrantStatus
GRLog.info(this.TAG,'startRecord checkPermissions then: ${grantStatus}");
if (grantStatus === abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED){
//PCM文件每帧容重(单位:字节数)计算公式:采样频率*采样位数*声道*时间/8
//每0.1秒发一个包
let 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
}
let audioCaptureInfo: audio.AudioCapturerInfo = {
source: audio.SourceType.SOURCE_TYPE_MIC,
capturerFlags: 0
}
let audioCapture0ptions: audio.AudioCapturerOptions = {
streanInfo: audiostreamInfo,
capturerInfo: audioCaptureInfo
}
audio.createAudioCapturer(audioCaptureOptions, (error: BusinessError, capture: audio.AudioCapturer)=> {
if (error){
GRLog.info(this.TAG,'startRecord createAudioCapturer error: ${JSON.stringify(error)}');
//麦克风权限被拒绝
} else{
this.audioRecord = capture;
this.audioRecord.off('readData',this.audioCaptureStartReceiver);
this.audioRecord.on('readData',this.audioCaptureStartReceiver);
this.audioRecord.start();
this.mStartTime = systemDateTime.getTime(false);
}
})
}).catch(error: BusinessError) (
}
private audioCaptureStartReceiver = (result: ArrayBuffer)=>{
GRLog.info(this.TAG,'audioCaptureStartReceiver result:${result.byteLength}');
try{
//将录音时间间隔转化为秒
Let durationTime = systemDateTime.getTime(false) - this.mStartTime;
if (durationTime > this.mMaxRecordTime){
this.isRecordTimeOut = true;
// 建议说话时间不要过长,请重试
return;
}
this.tmpRecordBuffers.push(buffer.from(result.slice(0, result.bytel
this.tmpBufsize += result.byteLength;
//PCM文件每帧容里(单位:字节数)计算公式:采样频率*采样位数*声道*时间/8
if (this.tmpBufsize >= this.recordBufsize
||(this.isStopRecord
&&this.tmpBufsize > 0
&& this.tmpBufsize < this.recordBufsize
&& this.tmpRecordBuffers.length > 0)) {
let tmpResult= buffer.concat(this.tmpRecordBuffers).buffer;
Let read: number = tmpResult.byteLength;
// 将录制的语音块请求数据都添加进队列中,等待会话建立再发送
this.addAudio(tmpResult, read);
this.tmpRecordBuffers=[];
this.tmpBufsize =8;
}
}catch(e) {
}
}
----------------- end ---------------
后面会继续补充不足之处。
本文标签: HarmonyOS NEXT 语音搜索场景学习和总结
版权声明:本文标题:HarmonyOS NEXT 语音搜索场景学习和总结 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1748212457a2827066.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论