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)));
 }

实现:
这里写图片描述

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值