TextToSpeech QML Type
TextToSpeech 类型提供对文本到语音引擎的访问。更多
Import Statement: | import QtTextToSpeech |
属性
- engine : string
- engineCapabilities : enumeration
(since 6.6)
- engineParameters : map
- locale : locale
- pitch : double
- rate : double
- state : enumeration
- voice : Voice
- volume : double
信号
- aboutToSynthesize(number id)
(since 6.6)
- void errorOccurred(enumeration reason, string errorString)
- sayingWord(string word, int id, int start, int length)
(since 6.6)
方法
- list<string> availableEngines()
- list<Voice> availableLocales()
- list<Voice> availableVoices()
- enqueue(string utterance)
(since 6.6)
- enumeration errorReason()
- string errorString()
- list<voice> findVoices(map criteria)
(since 6.6)
- pause(BoundaryHint boundaryHint)
- resume()
- say(string text)
- stop(BoundaryHint boundaryHint)
详细说明
使用say() 开始向默认音频设备读取文本,使用stop(),pause() 和resume() 控制文本的读取。
TextToSpeech { id: tts volume: volumeSlider.value pitch: pitchSlider.value rate: rateSlider.value ... RowLayout { Button { text: qsTr("Speak") enabled: [TextToSpeech.Paused, TextToSpeech.Ready].includes(tts.state) onClicked: { tts.say(input.text) } } Button { text: qsTr("Pause") enabled: tts.state == TextToSpeech.Speaking onClicked: tts.pause() visible: tts.engineCapabilities & TextToSpeech.Capabilities.PauseResume } Button { text: qsTr("Resume") enabled: tts.state == TextToSpeech.Paused onClicked: tts.resume() visible: tts.engineCapabilities & TextToSpeech.Capabilities.PauseResume } ...
要将文本合成为 PCM 数据以便进一步处理,请使用 synthesize()。
要设置语音,请使用VoiceSelector 附加属性:
TextToSpeech { VoiceSelector.locale: Qt.locale("en_UK") VoiceSelector.gender: Voice.Male }
将使用符合所有指定条件的第一个语音。如果没有符合所有条件的语音,那么语音将不会改变。
或者,使用findVoices() 获取匹配语音列表,或使用availableVoices() 获取支持当前本地语言的语音列表。更改locale 属性,使用与输入文本语言和所需语音输出口音匹配的availableLocales() 中的一个。这将更改大多数平台上的可用语音列表。然后在voice 属性中使用其中一种可用语音。
并非每个引擎都支持所有功能。请使用 engineCapabilities() 函数测试哪些功能可用,并相应调整类型的使用。
注意: 引擎支持哪些本地语言和语音通常取决于操作系统配置。例如,在 macOS 上,最终用户可以通过系统偏好设置中的辅助功能面板安装语音。
属性文档
engine : string |
engineCapabilities : enumeration |
engineParameters : map |
该属性包含特定于引擎的参数。
另请参阅 engine 。
locale : locale |
pitch : double |
该属性包含语音音高,范围从 -1.0 到 1.0。
默认值 0.0 为正常语音音高。
rate : double |
该属性用于保存当前的语音速率,范围从-1.0 到 1.0。
默认值 0.0 为正常语速。
state : enumeration |
该属性表示语音合成器的当前状态。
onStateChanged: updateStateLabel(state) function updateStateLabel(state) { switch (state) { case TextToSpeech.Ready: statusLabel.text = qsTr("Ready") break case TextToSpeech.Speaking: statusLabel.text = qsTr("Speaking") break case TextToSpeech.Paused: statusLabel.text = qsTr("Paused...") break case TextToSpeech.Error: statusLabel.text = qsTr("Error!") break } }
另请参阅 QTextToSpeech::State,say(),stop() 和pause()。
voice : Voice |
volume : double |
该属性保存当前音量,范围从 0.0 到 1.0。
默认值是平台的默认音量。
信号文档
|
void errorOccurred(enumeration reason, string errorString) |
该信号在发生错误且state 已设置为TextToSpeech.Error
后发出。reason 参数指定了错误类型,errorString 提供了人类可读的错误描述。
注: 相应的处理程序是onErrorOccurred
。
另请参阅 state,errorReason() 和errorString()。
|
当word (即语句id 中start 和length 所指示的文本片段)被播放到音频设备时,就会发出该信号。
注: 该信号要求引擎具有WordByWordProgress 功能。
下面的代码突出显示了TextArea input
中所说的单词:
onSayingWord: (word, id, start, length)=> { input.select(start, start + length) }
注: 相应的处理程序是onSayingWord
。
该信号在 Qt 6.6 中引入。
另请参阅 QTextToSpeech::Capability 和say()。
方法文档
list<string> availableEngines() |
持有支持的文本到语音引擎插件列表。
list<Voice> availableLocales() |
持有活动engine 支持的本地语言列表。
list<Voice> availableVoices() |
持有当前locale 可用的语音列表。
|
将utterance 添加到要发言的文本队列中,并开始发言。
如果引擎的state 当前是Ready
,则utterance 将立即被说出。否则,引擎将在说完当前文本后开始说utterance 。
每次引擎进入队列中的下一个文本条目时,都会发出aboutToSynthesize() 信号。这样,应用程序就可以跟踪进度,并在最后一刻更改语音属性。
调用stop() 会清除队列。
该方法在 Qt 6.6 中引入。
另请参阅 say()、stop() 和aboutToSynthesize()。
enumeration errorReason() |
返回引擎报错的原因。
另请参阅 QTextToSpeech::ErrorReason 。
string errorString() |
返回当前引擎的错误信息。
|
返回与所有指定criteria 匹配的声音列表。
criteria 是一个从声音属性名称到属性值的映射,支持搜索条件的组合,如
let daniel = tts.findVoices({ "name": "Daniel" }) let maleEnglish = tts.findVoices({ "gender": Voice.Male, "language": Qt.locale('en') })
此方法在 Qt 6.6 中引入。
另请参阅 VoiceSelector 。
pause(BoundaryHint boundaryHint) |
在boundaryHint 处暂停当前语音。
是否尊重boundaryHint 取决于engine 。
另请参阅 resume(),QTextToSpeech::BoundaryHint, 和PauseResume 。
say(string text) |
开始合成text 。
该函数以异步方式开始合成语音,并将文本读到默认的音频输出设备上。
RowLayout { Button { text: qsTr("Speak") enabled: [TextToSpeech.Paused, TextToSpeech.Ready].includes(tts.state) onClicked: { tts.say(input.text) } }
注意: 在开始读取最近合成的文本之前,所有正在进行的读取都会停止。
当前状态可通过state 属性获得,一旦开始朗读,该属性将被设置为QTextToSpeech::Speaking 。阅读完成后,state 将被设置为QTextToSpeech::Ready 。
stop(BoundaryHint boundaryHint) |
在boundaryHint 停止当前读取,并清除队列中的待读文本。
阅读无法恢复。是否尊重boundaryHint 取决于引擎。
另请参阅 say(),enqueue(),pause() 和QTextToSpeech::BoundaryHint 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.