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

demo截图

插件说明:

最新版插件已支持IOS!

由于讯飞官方限制,appid和sdk必须对应,所以使用该插件只替换下自己的appid是不行的。需要联系本人,使用你提供的sdk为你打离线包!

由于插件已支持安卓和ios双端,所以总体价格有所上涨,考虑到部分同学可能只需要一端,那么联系本人时可说明是否只打一端,只打一端的话,价格就会比较低!

插件地址

1.使用方法:

  • 1.引入插件:
const sr = uni.requireNativePlugin("YL-SpeechRecognition")
  • 2.初始化(注意:科大讯飞的appid,需要自己去科大讯飞官网申请)
//初始化
sr.init("6005f95c");//需要替换为自己的
//创建文字转语音对象
sr.createTts(code => {
    
    }); //0成功
//创建语音转文字对象
sr.createIat(code => {
    
    }); //0成功
  • 3.语音合成:
sr.textToVoice(text,res=>{
    
    })
  • 4.语音听写:
sr.voiceToText(res=>{
    
    })
  • 5.其它可供调用的方法:
sr.setSpeaker("aisjiuxu");//设置发音人(可能收费,自己在讯飞后台配置)
sr.setVadBos(10 * 1000);//设置语音听写前端点超时时间ms(最大10s)
sr.setVadEos(10 * 1000);//设置语音听写后端点超时时间ms(最大10s)

代码示例:

<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 => {
    
    
			}); //0成功
			//创建语音转文字对象
			sr.createIat(code => {
    
    
			}); //0成功

			// sr.setSpeaker("aisjiuxu");//设置语音播放者
			// sr.setVadBos(10 * 1000);//设置录音前端点超时时间(最大10s)
			// sr.setVadEos(10 * 1000);//设置录音后端点超时时间(最大10s)
			// sr.getIatPath(path=>{//获取语音听写录音文件路径
			// });
		},
		destroyed() {
    
    
			sr.stopSpeaking();
			sr.stopListening();
		},
		methods: {
    
    
			textToVoice() {
    
    
				if (this.toVoiceStatus == '未开始' || this.toVoiceStatus == "朗读完成" || this.toVoiceStatus == "朗读停止") {
    
    
					sr.textToVoice(this.text, res => {
    
    
						console.log(JSON.stringify(res));
						var data = res;
						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(res => {
    
    
					console.log(JSON.stringify(res));
					var data = res;
					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

猜你喜欢

转载自blog.csdn.net/baiyuliang2013/article/details/130925332