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 );