百度、腾讯云实时语音识别api对接

多平台实时翻译价格对比:

百度:在这里插入图片描述
讯飞:
在这里插入图片描述
有道翻译:
在这里插入图片描述
腾讯云:
在这里插入图片描述
根据以上价格对比,腾讯云和百度的价格相比较便宜,所以两个平台实时语音翻译的demo都分享一下。


百度

需要自己提前申请好相关的key

<template>
	<view class="test_page">
		-----{{vtext}}-----
		<view>
			<button @tap="startRecord">开始录音</button>
			<button @tap="endRecord">停止录音</button>
		</view>
	</view>
</template>

<script>
	const recorderManager = uni.getRecorderManager();
	const recorderConfig = {
		duration: 600000,
		frameSize: 5, //指定当录音大小达到5KB时触发onFrameRecorded
		format: 'PCM',
		//文档中没写这个参数也可以触发onFrameRecorded的回调,不过楼主亲测可以使用
		sampleRate: 16000,
		encodeBitRate: 96000,
		numberOfChannels: 1
	}
	export default {
		data() {
			return {
				vtext: '',
				socketTask: null,
				is_open_socket: false,
			}
		},
		onShow() {
			this.voice_initRecord()
		},
		beforeDestroy() {
			this.closeSocket()
		},
		methods: {
			startRecord() {
				this.connectSocketInit()
			},
			endRecord() {
				this.senEndCom()
			},
			voice_initRecord(){
				const _this = this
				recorderManager.onFrameRecorded(function(res) {
					let data = res.frameBuffer
					_this.ReatimeSend(data)
				})
			},
			// 实时发送录音包文件
			ReatimeSend(data) {
				this.socketTask.send({
					data: data,
					success(res) {
						console.log('发送数据帧成功')
					}
				})
			},
			// 发送开始录音帧
			senStartCom() {
				let config = {
					type: "START",
					data: {
						appid: 63690742, //百度实时语音识别appid
						appkey: "百度实时语音识别key", //百度实时语音识别key
						dev_pid: 15372,
						cuid: "cuid-1",
						format: "pcm",
						sample: 16000
					}
				}
				this.socketTask.send({
					data: JSON.stringify(config),
					success(res) {
						console.log('发送开始帧成功')
					}
				})
			},
			// 处理websocket返回内容
			itSocketData(res) {
				let result = JSON.parse(res.data.replace('\n', ''))
				if (result.type == 'MID_TEXT') {
					this.vtext = result.result
					console.log('开始数据,', result.result);
				}
				if (result.type == 'FIN_TEXT') {
					this.vtext = result.result
					console.log('结束数据,', result.result);
					this.senEndCom()
				}
			},
			// 进入这个页面的时候创建websocket连接【整个页面随时使用】
			connectSocketInit() {
				const _this = this
				//这里的sn是百度实时语音用于排查日志,这里我图方便就用时间戳了
				const sn = new Date().getTime()
				recorderManager.start(recorderConfig)
				this.socketTask = uni.connectSocket({
					url: 'wss://vop.baidu.com/realtime_asr?sn=' + sn,
					protocols: ['websocket'],
					success(data) {
						console.log("websocket连接已打开");
					},
				});

				// 消息的发送和接收必须在正常连接打开中,才能发送或接收【否则会失败】
				this.socketTask.onOpen((res) => {
					_this.is_open_socket = true;
					console.log('WebSocket连接打开')
					//发送数据帧
					_this.senStartCom()
					
					_this.socketTask.onMessage((res) => {
						this.itSocketData(res);
					});
				})
				// 这里仅是事件监听【如果socket关闭了会执行】
				this.socketTask.onClose(() => {
					console.log("socket-已关闭");
					_this.is_open_socket = false;
				})
			},
			// 发送结束录音帧
			senEndCom() {
				let _this = this
				let config = {
					type: "FINISH"
				}
				recorderManager.stop()
				this.socketTask.send({
					data: JSON.stringify(config),
					success(res) {
						wx.hideLoading()
						console.log('发送结束帧成功')
						_this.closeSocket()
					}
				})
			},
			// 关闭websocket【离开这个页面的时候执行关闭】
			closeSocket() {
				let _this = this
				if(!this.is_open_socket) return
				this.socketTask.close({
					success(res) {
						_this.is_open_socket = false;
						console.log("关闭成功", res)
					},
					fail(err) {
						console.log("关闭失败", err)
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.test_page {}
</style>

腾讯云:

添加腾讯云智能语音插件
在这里插入图片描述
在manifest.json中添加插件配置

"plugins" : {
 	"QCloudAIVoice": {
	    "version": "2.3.2",
	    "provider": "wx3e17776051baf153"
	}
},

在腾讯云平台申请相关key参数

<template>
	<view class="test_page">
		-----{{vtext}}-----
		<view>
			<button @tap="voiceStart">开始录音</button>
			<button @tap="voiceEnd">停止录音</button>
		</view>
	</view>
</template>

<script>
	let plugin = requirePlugin("QCloudAIVoice");
	let speechRecognizerManager = plugin.speechRecognizerManager();    
	export default {
		data() {
			return {
				vtext: '',
			}
		},
		onLoad() {
			// 请在页面onLoad时初始化好下列函数并确保腾讯云账号信息已经设置
			this.voice_initRecord()
		},
		methods: {
			voiceStart(){
				// 需要开始识别时调用此方法
				const params = {
				      signCallback: null, // 鉴权函数
				      // 用户参数
				      secretkey: '申请的secretkey',
				      secretid:  '申请的secretid',
				      appid: '腾讯云账号appid',  // 腾讯云账号appid(非微信appid)
				      token: '',  // 选填参数,若密钥为临时密钥,需传此参数。
				      // 录音参数
				      duration: 1000 * 60,
				      frameSize: 0.32,  //单位:k
				
				      // 实时识别接口参数
				      engine_model_type : '16k_zh',
				      // 以下为非必填参数,可跟据业务自行修改
				      // hotword_id : '08003a00000000000000000000000000',
				      // needvad: 0,
				      // filter_dirty: 1,
				      // filter_modal: 2,
				      // filter_punc: 0,
				      // convert_num_mode : 1,
				      // word_info: 2,
				      // vad_silence_time: 200
				};
				speechRecognizerManager.start(params);
			},
			voiceEnd(){
				// 需要停止识别时调用此方法
				speechRecognizerManager.stop();
			},
				
				
				
			voice_initRecord(){
				// 开始识别
				speechRecognizerManager.OnRecognitionStart = (res) => {
				  console.log('开始识别');
				}
				// 一句话开始
				speechRecognizerManager.OnSentenceBegin = (res) => {
				  console.log('一句话开始')
				  this.vtext = res.result.voice_text_str
				}
				// 识别变化时
				speechRecognizerManager.OnRecognitionResultChange = (res) => {
				  console.log('识别变化时')
				  this.vtext = res.result.voice_text_str
				}
				// 一句话结束
				speechRecognizerManager.OnSentenceEnd = (res) => {
				  console.log('一句话结束', res.result.voice_text_str)
				  this.vtext = res.result.voice_text_str
				}
				// 识别结束
				speechRecognizerManager.OnRecognitionComplete = (res) => {
				  console.log('识别结束');
				}
				// 识别错误
				speechRecognizerManager.OnError = (res) => {
				  // code为6001时,国内站用户请检查是否使用境外代理,如果使用请关闭。境外调用需开通国际站服务
				  console.log('识别失败', res);
				}
				// 录音结束(最长10分钟)时回调
				speechRecognizerManager.OnRecorderStop = (res) => {
				  console.log('录音结束');
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	.test_page {}
</style>
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Uniapp是一款基于Vue.js框架开发的跨平台应用开发框架,它可以将我们的Vue代码编译成不同平台的原生应用。而腾讯云人脸识别活体检测是一项用于识别人脸并验证其真实性的技术,可以用于安全认证、人脸支付等场景。 在Uniapp对接腾讯云人脸识别活体检测,我们可以按以下步骤进行: 1. 注册并创建腾讯云账号,开通人脸识别服务,并获取到相关的API密钥和访问密钥。 2. 在Uniapp项目中引入相关的插件或库,比如uni-app-faceid插件,该插件是为了方便Uniapp腾讯云人脸识别服务进行交互而开发的。 3. 在Uniapp的页面中,使用插件提供的组件或方法,调用腾讯云人脸识别服务的接口,传递待识别的人脸图像数据。 4. 接收腾讯云人脸识别服务返回的结果,根据返回的结果判断活体检测是否通过。 需要注意的是,在使用腾讯云人脸识别活体检测时,我们需要保证传入的人脸图像质量较高,且能够满足腾讯云人脸识别服务的要求,例如需要清晰可见的人脸图像。 除了以上步骤,根据具体需求,我们还可以根据腾讯云人脸识别服务的其他功能,如人脸比对、人脸搜索等,进行更高级的功能扩展。 总之,Uniapp腾讯云人脸识别活体检测的对接,需要通过引入插件或库来实现与腾讯云交互,并根据返回结果判断活体检测是否通过。这样我们就可以在Uniapp项目中应用腾讯云的人脸识别服务,实现更广泛的应用场景。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值