uniapp使用讯飞语音合成

uniapp使用讯飞语音合成
封装:

// A code block
var foo = 'bar';

var receiver = plus.android.implements(‘com.iflytek.cloud.SynthesizerListener’, {
onEvent: function(name) {
console.log(“onEvent”);
},
onSpeakBegin: function() {
console.log(“开始阅读”);
},
onSpeakPaused: function() {
console.log(" 暂停播放 ");
},
onSpeakResumed: function() {
console.log(“继续播放”);
},
onBufferProgress: function(percent, beginPos, endPos, info) {
console.log(“合成进度” + percent);
},
onSpeakProgress: function(percent, beginPos, endPos) {
console.log(“播放进度” + percent);
},
onCompleted: function(error) {
console.log(“播放完毕”);
}
});

var main = plus.android.runtimeMainActivity();

var SpeechUtility = plus.android.importClass(‘com.iflytek.cloud.SpeechUtility’);
//这里的appid填你自己申请的
SpeechUtility.createUtility(main, “appid=5faa97d8”);

var SynthesizerPlayer = plus.android.importClass(‘com.iflytek.cloud.SpeechSynthesizer’);

var play = SynthesizerPlayer.createSynthesizer(main, null);

function changeEnCode(str) {
let res = []
for (let i = 0; i < str.length; i++) {
// 这个不是正常的encodeUnicode 颠倒了大小端的位置
res[i] = (“00” + str.charCodeAt(i).toString(16)).slice(-2) + (“00” + str.charCodeAt(i).toString(16)).slice(-4, -
2)
}
let strCode = “\u” + res.join("\u")
strCode = strCode.replace(/\/g, “%”)
return unescape(strCode)
}

export {
receiver,
main,
SpeechUtility,
SynthesizerPlayer,
play,
changeEnCode
}



导入到Vue页

import {receiver,main,SpeechUtility,SynthesizerPlayer,play,changeEnCode} from ‘…/…/…/common/IFLYTEK/speechSynthesis.js’ //这里是封装的路径


使用

play.startSpeaking(changeEnCode(“你想说的话”),null );


你可能感兴趣的:(uniapp,vue.js,javascript,web,app)