HarmonyOS NEXT 语音搜索场景学习和总结

​大部分app的搜索页面都已经支持语音搜索,以下简单介绍以下HarmonyOS在语音搜索上的使用和总结语音搜索需要在module.json5中配置麦克风权限:ohos.permission.HICROPHONE在调起麦克风之前要对该权限进行

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 ---------------

后面会继续补充不足之处。

发布者:admin,转转请注明出处:http://www.yc00.com/web/1748212429a4748652.html

相关推荐

  • HarmonyOS NEXT 语音搜索场景学习和总结

    ​大部分app的搜索页面都已经支持语音搜索,以下简单介绍以下HarmonyOS在语音搜索上的使用和总结语音搜索需要在module.json5中配置麦克风权限:ohos.permission.HICROPHONE在调起麦克风之前要对该权限进行

    4小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信