uniapp使用百度语音识别语音转文字 您所在的位置:网站首页 怎么在手机上使用语音转换文字 uniapp使用百度语音识别语音转文字

uniapp使用百度语音识别语音转文字

2024-07-14 20:11| 来源: 网络整理| 查看: 265

HBuilderX已支持讯飞语音识别和百度语音识别,这里讲下百度语音接入

一:首先是百度语音识别申请

这个步骤暂且省略,可以直接移步百度语音api页面自行申请,主要是为了获取语音权限以及AppID、API Key、Secret Key三个参数,可从下图这里查看

二:获取到以上三个参数之后,对HBuilder进行配置

打开项目的manifest.json文件,在“App模块配置”项中勾选“Speech”,如下图输入上面获取到的三个参数

三:上面两个步骤完成后下面就可以品尝了,下面上菜

开始录音 结束录音 试听录音 转文字

{{resContent.result}}

//录音 const recorderManager = uni.getRecorderManager(); //播放录音 const innerAudioContext = uni.createInnerAudioContext(); innerAudioContext.autoplay = true; export default { components: { }, data() { return { title: 'Hello', token: '', adioFileData: '', adioSize: '', resContent: '', luyinStatus: true } }, onLoad() { let self = this; recorderManager.onStop(function(res) { //录音后的回调函数 console.log('recorder stop' + JSON.stringify(res)); self.voicePath = res.tempFilePath; self.Audio2dataURL(res.tempFilePath); }); }, methods: { startLuyin() { console.log('开始录音') recorderManager.start(); this.luyinStatus = false }, endLuyin() { console.log('录音结束'); recorderManager.stop(); this.luyinStatus = true }, startYuyin() { var _this = this; //获取token uni.request({ url: 'https://openapi.baidu.com/oauth/2.0/token', //仅为示例,并非真实接口地址。 data: { grant_type: 'client_credentials', client_id: '', client_secret: '', }, header: { 'content-type': 'application/json;charset=utf-8' //自定义请求头信息 }, success: (res) => { // console.log(JSON.stringify(res)); _this.token = res.data.access_token; // alert( _this.resContent) _this.PostData(); // _this.$refs.popup.open() } }); }, PostData() { var postData = { format: 'pcm', //语音文件的格式,pcm/wav/amr/m4a。不区分大小写。推荐pcm文件 rate: 16000, // 采样率,16000,固定值 此处文档参数16000,达不到这种高保真音频,故 使用8000 // dev_pid: 1537,//普通话 channel: 1, //声道数,仅支持单声道,请填写固定值 1 cuid: 'cuid', //用户唯一标识,用来区分用户,计算UV值。建议填写能区分用户的机器 MAC 地址或 IMEI 码,长度为60字符以内。 token: this.token, speech: this.adioFileData, //本地语音文件的的二进制语音数据 ,需要进行base64 编码。与len参数连一起使用。 len: this.adioSize //本地语音文件的的字节数,单位字节 init } console.log(JSON.stringify(postData) + '1111') //调用语音识别接口 uni.request({ url: 'http://vop.baidu.com/server_api', //仅为示例,并非真实接口地址。 data: postData, header: { 'content-type': 'Content-Type: audio/pcm;rate=16000' //自定义请求头信息 }, method: 'POST', success: (res) => { this.resContent = res.data console.log(JSON.stringify(res.data) + "识别结果"); // this.text = 'request success'; } }) }, Audio2dataURL(path) { var _this = this; plus.io.resolveLocalFileSystemURL(path, function(entry) { entry.file(function(file) { var reader = new plus.io.FileReader(); _this.adioSize = file.size; reader.onloadend = function(e) { console.log(e.target.result); _this.adioFileData = e.target.result.split(",")[1]; }; reader.readAsDataURL(file); _this.startYuyin() }, function(e) { alert(e.message) // mui.toast("读写出现异常: " + e.message); }) }) }, //播放 playVoice() { console.log('播放录音'); if (this.voicePath) { innerAudioContext.src = this.voicePath; innerAudioContext.play(); } } } } .content { display: flex; flex-direction: column; align-items: center; justify-content: center; } .logo { height: 200 rpx; width: 200 rpx; margin-top: 200 rpx; margin-left: auto; margin-right: auto; margin-bottom: 50 rpx; } .text-area { display: flex; justify-content: center; margin-top: 10 vh; } .text-area.recordingStyle { width: 100 px; height: 100 px; border-radius: 50 % ; text-align: center; display: flex; justify-content: center; align-items: center; font-size: 16 px; color: # fff; background: #409eff; } .Sound_btn{ display:flex; align-items:center; margin:10px 0 0 0; } .Sound_btn>button{ background:none; } .title { font-size: 36rpx; color: # 8 f8f94; } .reslut_box { width: 90 % ; background: #409eff10; margin:1em 5%; padding:0.5em 0; border-radius:10px; } .reslut_box p{ margin:0 20px; }

已经很详细了,各位吃好喝好

 



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有