前言:
看 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);
}