实例介绍
【实例简介】文本文件中的文本转化为语音输出
【实例截图】【核心代码】
#include "mainwindow.h"
#include <QLoggingCategory>
#include <QTextToSpeech>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
m_speech(0)
{
ui.setupUi(this);
setWindowTitle("语音播报");
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())
ui.engine->addItem(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();
}
/*设置语速-1.0到1.0*/
void MainWindow::setRate(int rate)
{
m_speech->setRate(rate / 10.0);
}
/*设置音高-1.0到1.0*/
void MainWindow::setPitch(int pitch)
{
m_speech->setPitch(pitch / 10.0);
}
/*设置音量0.0-1.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);
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论