uni app 的app 端调用tts 进行文字转语音
本文使用了谷歌引擎。我的资源中已上传
speckutils.js 代码如下
// #ifdef APP
const SpeechTTS = uni.requireNativePlugin("MT-TTS-Speech");
// #endiffunction init(callback) {// #ifdef APPconsole.log('>> tts: init = 111');SpeechTTS.init((res) => {console.log(JSON.stringify(res))callback(true)console.log('>> tts: init success');});console.log('>> tts: init = 222');// #endif
}function speckAndShwoTast(text) {uni.showToast({title: text,icon: 'none'});// #ifdef APPconst res = SpeechTTS.speak({text: text});console.log('>> tts: play result = ' + res);// #endif
}function speckText(text) {// #ifdef APPconst res = SpeechTTS.speak({text: text});console.log('>> tts: play result = ' + res);// #endif
}function speckstop() {// #ifdef APPSpeechTTS.stop((res) => {console.log("speckstop", JSON.stringify(res))})// #endif
}function destroy() {// #ifdef APPconsole.log("销毁tts")SpeechTTS.destroy()// #endif
}module.exports = {init: init,speckAndShwoTast: speckAndShwoTast,speckText: speckText,destroy: destroy,};
使用
import {
speckAndShwoTast
} from '../../utils/speckutils'
speckAndShwoTast("请输入正确的用户名密码")