Qt跨平臺文字轉語音

pro加上 QT += texttospeech

頭文件:

#ifndef TEXTTOSPEECH_H
#define TEXTTOSPEECH_H

#include <QObject>
#include <QTextToSpeech>

class TextToSpeech : public QObject
{
    Q_OBJECT
public:
    explicit TextToSpeech(QObject *parent = nullptr);

signals:

public slots:
    void speak(QString text);//需要轉語音的文本
private:
    QTextToSpeech *m_speech;
};

#endif // TEXTTOSPEECH_H

源文件:

#include "texttospeech.h"
#include <QLoggingCategory>


TextToSpeech::TextToSpeech(QObject *parent)
    : QObject(parent),
      m_speech(nullptr)
{
    QLoggingCategory::setFilterRules(QStringLiteral("qt.speech.tts=true \n qt.speech.tts.*=true"));
    foreach (QString engine, QTextToSpeech::availableEngines())
    {
        if(engine == "android"){//安卓平臺
            m_speech = new QTextToSpeech(engine, this);
        }else{
            m_speech = new QTextToSpeech(this);
        }
    }
    m_speech->setRate(0);
    m_speech->setPitch(0.7);
    m_speech->setVolume(1.0);
}



void TextToSpeech::speak(QString text)
{
    m_speech->say(text);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章