uniapp app 使用百度语音

本文介绍了如何在uni-app中集成百度语音服务,包括申请百度语音参数、在manifest.json配置参数,以及展示相关的页面和JS代码示例。特别指出在iOS和Android平台,音频格式的不同可能导致获取语音结果的问题。
摘要由CSDN通过智能技术生成

第一步: 百度语音申请

获取这三个参数

第二步:在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部分

  • 22
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
uniapp H5中使用百度语音识别API进行语音转文字可以按照以下步骤来进行: 1. 首先需要在百度AI开放平台上注册账号,创建应用,获取API Key和Secret Key。 2. 在uniapp项目中,引入百度语音识别API的SDK文件,可在百度AI开放平台上下载。 3. 在uniapp项目中,使用uni-app提供的录音组件进行录音,并将录音文件传递给百度语音识别API进行处理。 4. 将百度语音识别API返回的结果展示在页面上。 下面是一个简单的示例代码: ```html <!-- 引入录音组件 --> <uni-audio-recorder v-model="audioSrc" @success="onRecordSuccess" @error="onRecordError" /> <!-- 按钮触发识别 --> <button @click="startRecognize">开始识别</button> <div>{{result}}</div> ``` ```javascript export default { data() { return { // 录音文件路径 audioSrc: '', // 识别结果 result: '' } }, methods: { // 录音成功回调 onRecordSuccess(res) { this.audioSrc = res.tempFilePath }, // 录音失败回调 onRecordError(res) { uni.showToast({ title: '录音失败' }) }, // 开始识别 startRecognize() { // 引入百度语音识别API const SpeechRecognizer = uni.requireNativePlugin('uni-speech-recognizer') // 配置参数 const options = { apiKey: 'yourApiKey', secretKey: 'yourSecretKey', audioSource: this.audioSrc, audioFormat: 'wav', devPid: 1536 // 中文普通话 } // 调用API进行识别 SpeechRecognizer.recognizer(options, (res) => { this.result = res.result }, (err) => { uni.showToast({ title: '识别失败' }) }) } } } ``` 需要注意的是,使用uni-app提供的录音组件进行录音时,需要在manifest.json文件中添加权限声明: ```json { "permission": { "scope.record": { "desc": "用于录制和识别语音" } } } ``` 另外,需要在uni-app的manifest.json文件中添加nativePlugins声明,才能使用uni.requireNativePlugin引入百度语音识别API: ```json { "nativePlugins": [ { "name": "uni-speech-recognizer", "version": "1.0.0", "provider": "uni" } ] } ``` 希望这个示例能够帮助到您。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值