UniApp原生讯飞语音插件-YL-SpeechRecognition

demo截图

由于插件文档图片部分可能无法显示,可以移步到CSDN博客,查看完整文档:https://blog.csdn.net/baiyuliang2013/article/details/130925332

插件说明:

支持安卓IOS双端!

新版本已由必须联系本人打离线包,升级为线上购买后自主导入sdk打包,更方便快捷!当然,您在使用时遇到问题依然可以联系本人QQ:453503875,微信:同qq。

若您只需要安卓端,可使用:Uts版插件:https://ext.dcloud.net.cn/plugin?id=14794,此插件同样支持线上购买,自行替换讯飞sdk打包,方便快捷!

如果,您有其它语音相关需求,如唤醒,评测等,或者有其它插件定制需求,仍然可以联系本人!

插件使用说明:

1.先从讯飞官网下载sdk:

1).
在这里插入图片描述
2).

在这里插入图片描述

2.购买并选择导入项目后,配置原生插件:

在这里插入图片描述

3.在项目根目录创建原生插件目录(文件夹名称保持一致),将自己下载的讯飞sdk导入:

在这里插入图片描述

  • nativeplugins
    • YL-SpeechRecognition
      • android
        -libs
        • 安卓sdk
      • ios
        • iosSDK

安卓的sdk放入YL-SpeechRecognition/android/libs目录下,ios的framework放入YL-SpeechRecognition/ios目录下,文件夹名称一定要保持一致!!!

4.打自定义基座调试,或云打包发行!

1.使用方法:

  • 1.引入插件:
const sr = uni.requireNativePlugin("YL-SpeechRecognition")
  • 2.初始化(注意:科大讯飞的appid,需要自己去科大讯飞官网申请)
//初始化
sr.init("6005f95c");//体验测试阶段可以使用demo的,正式发布需要替换为自己的
//创建文字转语音对象
sr.createTts(code => {}); 
//创建语音转文字对象
sr.createIat(code => {});
  • 3.语音合成:
sr.textToVoice(text,res=>{})
  • 4.语音听写:
sr.voiceToText(res=>{})
  • 5.停止方法
sr.stopSpeaking();//停止语音合成
sr.stopListening();//停止语音听写
  • 6.销毁:(退出页面前销毁)
sr.destroy();
  • 7.其它可供调用的方法:
//语音合成
sr.setSpeaker("aisjiuxu");//设置发音人(可能收费,自己在讯飞后台配置)
sr.pauseSpeaking();//暂停
sr.resumeSpeaking();//恢复
sr.getTtsPath(path=>{});//获取语音文件路径

//语音听写
sr.stopListening();//停止
sr.setVadBos(10 * 1000);//设置语音听写前端点超时时间ms(最大10s,一般按默认即可)
sr.setVadEos(10 * 1000);//设置语音听写后端点超时时间ms(最大10s,一般按默认即可)
sr.setLanguage("");//设置听写语言,默认中文(zh_cn,en_us)
sr.getIatPath(path=>{});//获取语音文件路径

代码示例:

<template>
	<div style="padding: 20rpx;">
		<text style="display: block;margin-bottom: 20rpx;font-size: 20rpx;color: #FF0000;">文字转语音:</text>
		<text>{{text}}</text>
		<div style="display: flex;flex-direction: row;margin-top: 20rpx;">
			<text style="font-size: 20rpx;">状态:</text>
			<text style="margin-bottom: 20rpx;color: #FF0000;font-size: 20rpx;">{{toVoiceStatus}}</text>
		</div>
		<button type="primary" style="margin: 20rpx 0;" plain="true" @click="textToVoice()">语音朗读</button>
		<div style="display: flex;align-items: center;justify-content: space-between;flex-direction: row;">
			<button type="warn" plain="true" @click="stopSpeaking()" style="width: 160rpx;">停止</button>
			<button type="primary" plain="true" @click="pauseSpeaking()" style="width: 160rpx;">暂停</button>
			<button type="primary" plain="true" @click="resumeSpeaking()" style="width: 160rpx;">继续</button>
		</div>

		<text
			style="display: block;margin-top: 50rpx;;margin-bottom: 20rpx;font-size: 20rpx;color: #FF0000;">语音转文字:</text>
		<text style="display: block;margin-bottom: 20rpx;">{{transText}}</text>
		<div style="display: flex;flex-direction: row;">
			<text style="font-size: 20rpx;">状态:</text>
			<text style="margin-bottom: 20rpx;color: #FF0000;font-size: 20rpx;">{{toTextStatus}}</text>
			<text style="font-size: 20rpx;margin-left: 50rpx;">音量:</text>
			<text style="margin-bottom: 20rpx;color: #FF0000;font-size: 20rpx;">{{volume}}</text>
		</div>
		<button type="primary" style="margin: 20rpx 0;" plain="true" @click="voiceToText()">开始录音</button>
		<button type="primary" style="margin: 20rpx 0;" plain="true" @click="stopVoiceToText()">停止录音</button>
        <text style="margin-bottom: 20rpx;color: #FF0000;font-size: 20rpx;">{{path}}</text>
	</div>
</template>

<script>
	// 获取 module 
	const sr = uni.requireNativePlugin("YL-SpeechRecognition")

	export default {
		data() {
			return {
				text: "uni-app是一个使用 Vue.js开发所有前端应用的框架,开发者编写一套代码,可发布到iOS、Android、Web、以及各种小程序、快应用等多个平台。",
				toVoiceStatus: "未开始",
				transText: "",
				toTextStatus: "未开始",
				volume: 0,
				path:""
			}
		},
		mounted() {
			//初始化
			sr.init("6005f95c");
			//创建文字转语音对象
			sr.createTts(code => {}); 
			//创建语音转文字对象
			sr.createIat(code => {});
		},
		beforeDestroy() {
			sr.destroy();
		},
		methods: {
			textToVoice() {
				if (this.toVoiceStatus == '未开始' || this.toVoiceStatus == "朗读完成" || this.toVoiceStatus == "朗读停止") {
					sr.textToVoice(this.text, data => {
						console.log(JSON.stringify(data));
						switch (data.code) {
							case 1001:
								this.toVoiceStatus = "开始朗读"
								break;
							case 1002:
								this.toVoiceStatus = "暂停朗读"
								break;
							case 1003:
								this.toVoiceStatus = "继续朗读"
								break;
							case 1004:
								this.toVoiceStatus = "正在缓冲..."
								break;
							case 1005:
								this.toVoiceStatus = "正在朗读..."
								break;
							case 1006:
								this.toVoiceStatus = "朗读完成"
								break;
							case 1007:
								this.toVoiceStatus = "朗读停止"
								break;
						}
					});
				}
			},
			stopSpeaking() {
				sr.stopSpeaking();
			},
			pauseSpeaking() {
				sr.pauseSpeaking();
			},
			resumeSpeaking() {
				sr.resumeSpeaking();
			},
			voiceToText() {
				let that = this;
				sr.voiceToText(data => {
					console.log(JSON.stringify(data));
					if (data.code == 1001) {
						that.toTextStatus = "倾听中,请说话..."
					} else if (data.code == 1006) {
						this.toTextStatus = "倾听完毕"
						sr.getIatPath(path=>{
							that.path=path;
						});
					} else if (data.code == 1007) {
						this.toTextStatus = "停止倾听"
					} else if (data.code == 1008) {
						this.volume = data.msg;
					} else if (data.code == 1009) { //结果
						this.transText = data.msg;
					} else if (data.code == 1010) { //error信息
					  //如果data.msg不为空,则属于报错
					  if(data.msg){
						  this.transText = data.msg;
					  }
					}
				})
			},
			stopVoiceToText() {
				sr.stopListening();
			}
		}
	}
</script>

各种回调状态,可参考以上案例代码!

需要注意的是,安卓和ios平台,在语音听写调用停止方法stopListening时,回调有些许不同:

安卓:1007->1009

IOS:1007->1006->1009->1010

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白玉梁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值