uniapp app 使用百度语音
console.log("获取的token:", res.data.access_token)1.百度语音在uniapp 中 ios 和 android 的音频格式 是不一样的,这一点很重要 ,弄错的话会导致获取不到值。第二步:在uniapp--- manifest.json 文件 -- APP模块配置 --- Speech模块。console.log("识别结果:", res.data)titl
·
第一步: 百度语音申请
获取这三个参数
第二步:在uniapp --- manifest.json 文件 -- APP模块配置 --- Speech模块
填写 对应的参数
第三步:直接上代码
页面部分
<view class="recordBut flex-con" @longpress="handleRecordStart" @touchend="handleRecordStop">
<view>
<u-icon name="mic" color="#485EF4" size="18"></u-icon>
</view>
<view>
<u--text :text="isRecording?'正在录音中...':'语音搜索'" align="center" color="#485EF4" size="16"></u--text>
</view>
</view>
js部分
data() {
return {
sendLock: true,
isRecording: false, // 是否正在录音
recorderManager: uni.getRecorderManager(),
adioFileData: '',
adioSize: '',
token: "",
micType: ""
}
},
watch: {
sendLock(newVal) {
const that = this
that.recorderManager.onStop(res => {
// #ifdef APP-PLUS
that.Audio2dataURL(res.tempFilePath)
// #endif
})
}
},
handleRecordStart(e) {
this.micType = uni.getSystemInfoSync().platform === 'ios' ? 'pcm' : 'amr'
// #ifdef APP-PLUS
this.recorderManager.start({
format: this.micType,
numberOfChannels: 1,
sampleRate: 16000
})
// #endif
this.sendLock = false
this.isRecording = true
},
//结束录音 (手指松开)时触发
handleRecordStop(e) {
this.isRecording = false
this.recorderManager.stop()
},
Audio2dataURL(path) {
var _this = this;
uni.showLoading({
title: "识别中...",
mask: true
})
plus.io.resolveLocalFileSystemURL(path, function(entry) {
entry.file(function(file) {
var reader = new plus.io.FileReader()
_this.adioSize = file.size;
reader.onloadend = function(e) {
_this.adioFileData = e.target.result.split(",")[1];
}
reader.readAsDataURL(file)
_this.startYuyin()
}, function(e) {
})
})
},
startYuyin() {
var _this = this;
uni.request({
url: 'https://openapi.baidu.com/oauth/2.0/token',
data: {
grant_type: 'client_credentials',
client_id: 'cWIS1ruIq0heapf34C02GMms',
client_secret: 'kko21ByS7pmfFB8bMpmtacCHFSztZGge',
},
header: {
'Content-Type': 'application/json'
},
success: (res) => {
console.log("获取的token:", res.data.access_token)
_this.token = res.data.access_token
_this.PostData()
}
})
},
PostData() {
const that = this
var postData = {
format: that.micType,
rate: 16000,
dev_pid: 80001,
channel: 1,
cuid: uni.getStorageSync('unid'),
token: this.token,
speech: this.adioFileData,
len: this.adioSize
}
//调用语音识别接口
uni.request({
url: 'https://vop.baidu.com/pro_api',
data: postData,
header: {
'Content-Type': 'application/json'
},
method: 'POST',
success: (res) => {
uni.hideLoading()
console.log("识别结果:", res.data)
}
})
}
总结:
1.百度语音 在uniapp 中 ios 和 android 的音频格式 是不一样的,这一点很重要 ,弄错的话会导致获取不到值
更多推荐
已为社区贡献1条内容
所有评论(0)