Qt的QTextToSpeech类实现语音播报功能 您所在的位置:网站首页 语音播报用什么软件 Qt的QTextToSpeech类实现语音播报功能

Qt的QTextToSpeech类实现语音播报功能

2024-06-29 10:39| 来源: 网络整理| 查看: 265

看 qt 的 demo 看到一个播放语音的 玩了玩 还可以 就是太"傻瓜"的操作了 我以为能学到一些东西。

该工程存放在Qt安装目录下的

Examples\Qt-XX.XX.XX\speech

其中XX.XX.XX为Qt的版本号,如:5.14.1。

speech->say("你好");

这样就能说 你好

我这就不弄动图了 因为听不到声音

基本的功能

设置声音

设置速率

设置高低音

然后 有 播放引擎 是基于你系统的TTS 引擎 语言的话 可以选择 中文 英文 等 去系统里面可以设置 QTextToSpeech (Qt 5.8+ 才有 这个模块)

QTextToSpeech类提供了对文本到语音引擎的方便访问 使用say()开始合成文本。可以使用setLocale()指定语言。要在可用的声音之间进行选择,请使用setVoice()。语言和声音依赖于每个平台上可用的合成器。在Linux上,语音分配器是默认使用的。  

在 pro 加入 QT+= qtexttospeech

#include < QTextToSpeech >

这个代码 我看了一下 感觉没啥好看的 这个类 给封装的 很简单 写一些接口的使用吧 获取可用的引擎 QTextToSpeech::availableEngines()

foreach (QString engine, QTextToSpeech::availableEngines()) qDebug()setRate(rate / 10.0); } void MainWindow::setPitch(int pitch) { m_speech->setPitch(pitch / 10.0); } void MainWindow::setVolume(int volume) { m_speech->setVolume(volume / 100.0); } void MainWindow::stateChanged(QTextToSpeech::State state) { if (state == QTextToSpeech::Speaking) { ui.statusbar->showMessage("Speech started..."); } else if (state == QTextToSpeech::Ready) ui.statusbar->showMessage("Speech stopped...", 2000); else if (state == QTextToSpeech::Paused) ui.statusbar->showMessage("Speech paused..."); else ui.statusbar->showMessage("Speech error!"); ui.pauseButton->setEnabled(state == QTextToSpeech::Speaking); ui.resumeButton->setEnabled(state == QTextToSpeech::Paused); ui.stopButton->setEnabled(state == QTextToSpeech::Speaking || state == QTextToSpeech::Paused); } void MainWindow::engineSelected(int index) { QString engineName = ui.engine->itemData(index).toString(); delete m_speech; if (engineName == "default") m_speech = new QTextToSpeech(this); else m_speech = new QTextToSpeech(engineName, this); disconnect(ui.language, static_cast(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected); ui.language->clear(); // Populate the languages combobox before connecting its signal. QVector locales = m_speech->availableLocales(); QLocale current = m_speech->locale(); foreach (const QLocale &locale, locales) { QString name(QString("%1 (%2)") .arg(QLocale::languageToString(locale.language())) .arg(QLocale::countryToString(locale.country()))); QVariant localeVariant(locale); ui.language->addItem(name, localeVariant); if (locale.name() == current.name()) current = locale; } setRate(ui.rate->value()); setPitch(ui.pitch->value()); setVolume(ui.volume->value()); connect(ui.stopButton, &QPushButton::clicked, m_speech, &QTextToSpeech::stop); connect(ui.pauseButton, &QPushButton::clicked, m_speech, &QTextToSpeech::pause); connect(ui.resumeButton, &QPushButton::clicked, m_speech, &QTextToSpeech::resume); connect(m_speech, &QTextToSpeech::stateChanged, this, &MainWindow::stateChanged); connect(m_speech, &QTextToSpeech::localeChanged, this, &MainWindow::localeChanged); connect(ui.language, static_cast(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected); localeChanged(current); } void MainWindow::languageSelected(int language) { QLocale locale = ui.language->itemData(language).toLocale(); m_speech->setLocale(locale); } void MainWindow::voiceSelected(int index) { m_speech->setVoice(m_voices.at(index)); } void MainWindow::localeChanged(const QLocale &locale) { QVariant localeVariant(locale); ui.language->setCurrentIndex(ui.language->findData(localeVariant)); disconnect(ui.voice, static_cast(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected); ui.voice->clear(); m_voices = m_speech->availableVoices(); QVoice currentVoice = m_speech->voice(); foreach (const QVoice &voice, m_voices) { ui.voice->addItem(QString("%1 - %2 - %3").arg(voice.name()) .arg(QVoice::genderName(voice.gender())) .arg(QVoice::ageName(voice.age()))); if (voice.name() == currentVoice.name()) ui.voice->setCurrentIndex(ui.voice->count() - 1); } connect(ui.voice, static_cast(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected); }

原文链接:https://blog.csdn.net/weixin_42837024/article/details/105394412



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有