目的:实现语音读文字功能

.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include
<QTextToSpeech> namespace Ui { class MainWindow; } class MainWindow : public
QMainWindow { Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0);
~MainWindow();public: void init(); 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; };#endif // MAINWINDOW_H
.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QLoggingCategory>
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::
MainWindow),m_speech(0) { ui->setupUi(this); init(); } void MainWindow::init() {
QLoggingCategory::setFilterRules(QStringLiteral("qt.speech.tts=true \n
qt.speech.tts.*=true")); // Populate engine selection ui->engine->addItem(
"Default", QString("default")); foreach (QString engine, QTextToSpeech
::availableEngines()) ui->engine->addItem(engine, engine);
ui->engine->setCurrentIndex(0); engineSelected(0); // m_speech = new
QTextToSpeech(this); connect(ui->pushButtonSpeak,SIGNAL(clicked(bool)),this,SLOT
(speak())); connect(ui->SliderByPitch,SIGNAL(valueChanged(int)),this,SLOT
(setPitch(int))); connect(ui->SliderByRate,SIGNAL(valueChanged(int)),this,SLOT
(setRate(int))); connect(ui->SliderByVolume,SIGNAL(valueChanged(int)),this,SLOT
(setVolume(int))); connect(ui->engine,SIGNAL(currentIndexChanged(int)),this,SLOT
(engineSelected())); }MainWindow::~MainWindow() { delete ui; } void MainWindow
::speak() {if(m_speech->state()==QTextToSpeech::Ready) { m_speech->say("hello
world"); m_speech->say("zcf,i love you"); m_speech->say(
"现在语音聊天机器人是一度火热,网上也有其他编程软件的语音聊天机器人"); } } void MainWindow::stop() {
m_speech->stop(); } voidMainWindow::setRate(int rate) { m_speech->setRate(rate /
10.0); } void MainWindow::setPitch(int pitch) { m_speech->setPitch(pitch / 10.0
); } voidMainWindow::setVolume(int volume) { m_speech->setVolume(volume / 100.0
); } voidMainWindow::stateChanged(QTextToSpeech::State state) { if (state ==
QTextToSpeech::Speaking) { statusBar()->showMessage("Speech started..."); } else
if (state == QTextToSpeech::Ready) statusBar()->showMessage("Speech stopped...",
2000); else if (state == QTextToSpeech::Paused) statusBar()->showMessage(
"Speech paused..."); else statusBar()->showMessage("Speech error!");
ui->pushButtonpause->setEnabled(state ==QTextToSpeech::Speaking);
ui->pushButtonresume->setEnabled(state ==QTextToSpeech::Paused);
ui->pushButtonStop->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,SIGNAL
(currentIndexChanged(int)),this,SLOT(languageSelected()));
ui->language->clear(); //Populate the languages combobox before connecting its
signal.QVector<QLocale> locales = m_speech->availableLocales(); QLocale current
= m_speech->locale(); foreach (constQLocale &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->SliderByRate->value()); setPitch(ui->
SliderByPitch->value()); setVolume(ui->SliderByVolume->value());
connect(ui->pushButtonStop,SIGNAL(clicked(bool)),m_speech,SLOT(stop()));
connect(ui->pushButtonpause,SIGNAL(clicked(bool)),m_speech,SLOT(pause()));
connect(ui->pushButtonresume,SIGNAL(clicked(bool)),m_speech,SLOT(resume()));
connect(m_speech,SIGNAL(stateChanged(QTextToSpeech::State)),this,SLOT
(stateChanged(QTextToSpeech::State))); connect(m_speech,SIGNAL(localeChanged(
QLocale)),this,SLOT(localeChanged(QLocale))); connect(ui->language,SIGNAL
(currentIndexChanged(int)),this,SLOT(languageSelected(int)));
localeChanged(current); } voidMainWindow::languageSelected(int language) {
QLocale locale = ui->language->itemData(language).toLocale();
m_speech->setLocale(locale); } voidMainWindow::voiceSelected(int index) {
m_speech->setVoice(m_voices.at(index)); } voidMainWindow::localeChanged(const
QLocale &locale) { QVariant localeVariant(locale);
ui->language->setCurrentIndex(ui->language->findData(localeVariant));
disconnect(ui->engine,SIGNAL(currentIndexChanged(int)),this,SLOT
(voiceSelected(int))); ui->engine->clear(); m_voices =
m_speech->availableVoices();QVoice currentVoice = m_speech->voice(); foreach
(constQVoice &voice, m_voices) { ui->engine->addItem(QString("%1 - %2 - %3"
).arg(voice.name()) .arg(QVoice::genderName(voice.gender())) .arg(QVoice
::ageName(voice.age())));if (voice.name() == currentVoice.name())
ui->engine->setCurrentIndex(ui->engine->count() -1); } connect(ui->engine,SIGNAL
(currentIndexChanged(int)),this,SLOT(voiceSelected(int))); }
实现:

友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信