当前位置:网站首页>Qt的QTextToSpeech类实现语音播报功能
Qt的QTextToSpeech类实现语音播报功能
2022-07-27 17:20:00 【荆楚闲人】
看 qt 的 demo 看到一个播放语音的 玩了玩 还可以
就是太"傻瓜"的操作了 我以为能学到一些东西
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()<<engine;
类的实例化
如果不指定引擎 可以选择默认的
QTextToSpeech * m_speech = new QTextToSpeech();
可以用我们上面选择的可用的引擎的名字传入
QTextToSpeech * m_speech = new QTextToSpeech(engineName);

setRate(double)
可以设置 速率 高低音 音量
此属性保存当前语音速率,范围从-1.0到1.0。默认值0.0是正常的语音流。
setPitch(double)
此属性保存语音音高,范围从-1.0到1.0。默认的0.0是正常的语音音高。
setVolume(double)
此属性保存当前音量,范围从0.0到1.0。默认值是平台的默认音量。
void setVoice(const QVoice &voice);
设置 声音是谁的 我看window下 有个男声音和女声音
设置声音使用。
注意:在某些平台上,设置语音会更改其他语音属性,如地区、音高等。这些变化触发了信号的发射。
void setLocale(const QLocale &locale);
设置语言的语种 有中文 英文啥的
将语言环境设置为给定的语言环境。默认是系统语言环境。
注意:属性区域设置的Setter函数。
播放语音 void say(const QString &text);
传入 字符串
比如 say(“hello world”) 语音里就说 hello world
它是异步的开始合成文章。这个函数将开始异步读取文本。使用state属性可以使用当前状态。一旦合成完成,就会发出stateChanged()信号,该信号处于就绪状态。
一些状态 (就绪 speaking 暂停中 等)

官方demo 用的一些 接口 我上面都说了
其他的都是一些 界面和逻辑的代码
看一下也可以
下面把 Qt demo 的整个 代码贴一下
#include <QtWidgets/qmainwindow.h>
#include "ui_mainwindow.h"
#include <QTextToSpeech>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
public slots:
void speak();
void stop();
void setRate(int);
void setPitch(int);
void setVolume(int volume);
void stateChanged(QTextToSpeech::State state);
void engineSelected(int index);
void languageSelected(int language);
void voiceSelected(int index);
void localeChanged(const QLocale &locale);
private:
Ui::MainWindow ui;
QTextToSpeech *m_speech;
QVector<QVoice> m_voices;
};
.cpp
#include "mainwindow.h"
#include <QLoggingCategory>
#include <QDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_speech(0)
{
ui.setupUi(this);
QLoggingCategory::setFilterRules(QStringLiteral("qt.speech.tts=true \n qt.speech.tts.*=true"));
// Populate engine selection list
ui.engine->addItem("Default", QString("default"));
foreach (QString engine, QTextToSpeech::availableEngines())
qDebug()<<"engine:"<<engine;
ui.engine->setCurrentIndex(0);
engineSelected(0);
connect(ui.speakButton, &QPushButton::clicked, this, &MainWindow::speak);
connect(ui.pitch, &QSlider::valueChanged, this, &MainWindow::setPitch);
connect(ui.rate, &QSlider::valueChanged, this, &MainWindow::setRate);
connect(ui.volume, &QSlider::valueChanged, this, &MainWindow::setVolume);
connect(ui.engine, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::engineSelected);
}
void MainWindow::speak()
{
m_speech->say(ui.plainTextEdit->toPlainText());
}
void MainWindow::stop()
{
m_speech->stop();
}
void MainWindow::setRate(int rate)
{
m_speech->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<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::languageSelected);
ui.language->clear();
// Populate the languages combobox before connecting its signal.
QVector<QLocale> 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<void (QComboBox::*)(int)>(&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<void (QComboBox::*)(int)>(&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<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), this, &MainWindow::voiceSelected);
}
原文链接:https://blog.csdn.net/weixin_42837024/article/details/105394412
边栏推荐
- The go zero singleton service uses generics to simplify the registration of handler routes
- 深度主动学习综述2020
- 归一化(Normalization)和标准化(Standardization)
- Virtualbox:ssh connection
- AutoCompleteTextView(输入框预匹配)
- 贪心
- [basic knowledge of deep learning - 41] quick start learning materials for deep learning
- What's new in helix QAC 2022.2, the ace code static testing tool (2)
- Systemservice (system service)
- No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
猜你喜欢

Intent (whether there is return value to jump)

Combinatorics -- permutation and combination

JS 事件监听 鼠标 键盘 表单 页面 onclick onkeydown onChange

ToggleButton(按钮开关)
![[basic knowledge of deep learning - 37] solve the imbalance between positive and negative samples](/img/71/4052607951eb52862a6fd36366f216.png)
[basic knowledge of deep learning - 37] solve the imbalance between positive and negative samples

Intent(有无返回值得跳转)

RadioGroup(单选框)
![[basic knowledge of deep learning - 46] Bayesian theorem and conditional probability formula](/img/9f/b9d7503404e068495fd8613df29366.png)
[basic knowledge of deep learning - 46] Bayesian theorem and conditional probability formula

Sqlife (database)

FileOutputStream(文件储存)与FileInputStream(文件读取)
随机推荐
C193:评分系统
Matplotlib(基本用法)
[RCTF2015]EasySQL-1|SQL注入
Surpass Huawei? Ericsson has won more than 75 5g commercial contracts
[basic knowledge of deep learning - 43] concept of odds ratio
Oracle 简单的高级查询
mysql学习录(三)多表查询、子查询、分页查询、case语句、单行函数
Intel launched the world's smallest high-resolution lidar, priced at only $349
DCM11- 根据标识符写入数据服务 ($2E)的功能和配置【基于DaVinci Configurator Classic】
JVM概述和内存管理(未完待续)
中国业务型CDP白皮书 | 爱分析报告
【C#】正序、逆序、最大值、最小值和平均值
ContextMenu(上下文菜单)
pytorch tensor的基本函数
No experts! Growth secrets for junior and intermediate programmers and "quasi programmers" who are still practicing in Universities
顶级“黑客”能厉害到什么地步?无信号也能上网,专家:高端操作!
FileOutputStream(文件储存)与FileInputStream(文件读取)
Fileoutputstream (file storage) and FileInputStream (file reading)
Systemservice (system service)
Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night