更新记录

1.0.0(2024-04-09)

文字转语音实时播放,可设置语调、语速、语言等


平台兼容性

Vue2 Vue3
App 快应用 微信小程序 支付宝小程序 百度小程序 字节小程序 QQ小程序
Android:4.4,iOS:9 × × × × × ×
钉钉小程序 快手小程序 飞书小程序 京东小程序
× × × ×
H5-Safari Android Browser 微信浏览器(Android) QQ浏览器(Android) Chrome IE Edge Firefox PC-Safari
× × × × × × × × ×

文字转语音

  • 文字转语音实时播放,可设置语调、语速、语言等
  • 支持定制(QQ252797991)
  • 集成本插件,集成步骤可参考(https://www.jianshu.com/p/830ccc503e29)[https://www.jianshu.com/p/830ccc503e29]或官网

    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 "":
                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)
    }

})
  • 获取语言列表(仅支持Android)

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)
  • 关闭(仅支持Android)

textToSpeak.shutdown()

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

使用中有什么不明白的地方,就向插件作者提问吧~ 我要提问