多平台实时翻译价格对比:
百度:
讯飞:
有道翻译:
腾讯云:
根据以上价格对比,腾讯云和百度的价格相比较便宜,所以两个平台实时语音翻译的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>