1. 程式人生 > >Qt --實現語音讀文字功能

Qt --實現語音讀文字功能

目的:實現語音讀文字功能

.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QTextToSpeech>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    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(); } 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) { 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 (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->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); } 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->engine,SIGNAL(currentIndexChanged(int)),this,SLOT(voiceSelected(int))); ui->engine->clear(); m_voices = m_speech->availableVoices(); QVoice currentVoice = m_speech->voice(); foreach (const QVoice &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))); }

實現:
這裡寫圖片描述