Qt 播放语音 QTextToSpeech

前言:

看 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 的整个 代码贴一下

.h

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

发布了194 篇原创文章 · 获赞 443 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/weixin_42837024/article/details/105394412