更新记录
1.0.1(2024-10-23)
- 修复新版本HB回调一次问题
1.0.0(2024-04-09)
文字转语音实时播放,可设置语调、语速、语言等
平台兼容性
App |
快应用 |
微信小程序 |
支付宝小程序 |
百度小程序 |
字节小程序 |
QQ小程序 |
HBuilderX 3.6.8,Android:4.4,iOS:9,HarmonyNext:不确定 |
× |
× |
× |
× |
× |
× |
钉钉小程序 |
快手小程序 |
飞书小程序 |
京东小程序 |
× |
× |
× |
× |
H5-Safari |
Android Browser |
微信浏览器(Android) |
QQ浏览器(Android) |
Chrome |
IE |
Edge |
Firefox |
PC-Safari |
× |
× |
× |
× |
× |
× |
× |
× |
× |
TTS文字转语音
import {
UTSTextToSpeak
} from "@/uni_modules/wrs-uts-texttospeak"
let textToSpeak = new UTSTextToSpeak()
textToSpeak.initSpeak()
textToSpeak.setCallback((resp) => {
var opt = resp.opt
if (this.isAndroid) {
switch (opt) {
case "onStart":
this.showMsg("开始播放")
break;
case "onDone":
this.showMsg("播放完成")
break;
case "onError":
this.showMsg("播放出错")
break;
default:
break;
}
} else {
switch (opt) {
case "didStart":
this.showMsg("开始播放")
break;
case "didFinish":
this.showMsg("播放完成")
break;
case "didPause":
this.showMsg("播放暂停")
break;
case "didCancel":
this.showMsg("播放取消")
break;
case "willSpeakRangeOfSpeechString":
this.showMsg("willSpeakRangeOfSpeechString")
break;
default:
break;
}
}
this.showMsg(JSON.stringify(resp))
})
var params = {}
params.text = this.text
if (this.isAndroid) {
if (this.voice) {
params.voice = this.voice // 声音
}
if (this.language) {
params.language = { // 语言
language: this.language.language,
country: this.language.country
}
}
params.pitch = parseFloat(this.pitch) // 音调 [0.5~2.0]
params.speechRate = parseFloat(this.speechRate) // 语速[0.1~2.0]
params.params = {
utteranceId: Date.now() + "" // 声音ID,自定义
}
params.queueMode = 0 // 队列模式,0: 中断当时的播报,播报新的语音 1: 添加到当前任务之后
} else {
params.pitchMultiplier = parseFloat(this.pitch) // 音调 [0.5 - 2]
params.rate = parseFloat(this.speechRate) // 语速[0~1]
params.volume = 1 // 声音大小[0~1]
if (this.voiceIdentifier) {
params.voiceIdentifier = this.voiceIdentifier
}
}
console.log(JSON.stringify(params))
textToSpeak.speak(params)
let voices = textToSpeak.getVoices()
var voicesArray = JSON.parse(voices)
var itemList = []
var length = voicesArray.length
for (var i = 0; i < length; i++) {
var model = voicesArray[i]
var title = "--"
if (this.isAndroid) {
title = model.name + "(" + model.locale.displayName + ")"
} else {
title = model.name + "(" + model.language + ")"
}
itemList.push(title)
}
this.showActionSheet(itemList, (i) => {
if (this.isAndroid) {
this.voice = voicesArray[i].name
} else {
this.voiceIdentifier = voicesArray[i].identifier
console.log(this.voiceIdentifier)
}
})
let locales = textToSpeak.getAvailableLocales()
var localesArray = JSON.parse(locales)
var itemList = []
var length = localesArray.length
for (var i = 0; i < length; i++) {
var model = localesArray[i]
var title = model.displayName + "(" + model.displayLanguage + ")"
itemList.push(title)
}
this.showActionSheet(itemList, (i) => {
var model = localesArray[i]
this.language = {
language: model.language,
country: model.country
}
})
var params = {}
params.boundary = 0 // 仅支持iOS, 0: 马上停止 1: 说完一个单词再停止
textToSpeak.stop(params)
textToSpeak.shutdown()