QT calls Baidu speech rest api for speech synthesis and rest speech synthesis
QT calls Baidu speech rest api for Speech Synthesis
1. First click on the link http://yuyin.baidu.com/docs/tts
Click access_token to obtain the access_token. The detailed steps are provided.
Write down the link and use it in the QT program. The link after tex is followed by the text to be converted into speech, and the tok is followed by the obtained access_token.
2. Open Qt Creator and create a QWidget application. The rendering interface is as follows:
3. Functions for obtaining the voice button slot are as follows:
void Widget::on_pushButton_clicked(){ QByteArray url="http://tsn.baidu.com/text2audio?"; url.append(QString("&lan=zh&cuid=***&ctp=1&tok=***&pit=8&per=3")); url.append("&tex="); url.append(QUrl::toPercentEncoding(ui->textEdit->toPlainText())); qDebug()<<url; player->setMedia(QUrl::fromLocalFile(url)); player->play();}
Player is an object of the QMediaPlayer class and is declared in widget. h.
private:
QMediaPlayer * player;
Add the following code to the constructor:
Player = new QMediaPlayer (this );
Replace the cuid with the mac address of your computer, and add the access_token obtained in step 1 to the tok.
Below are all the code
Widget. h
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QMediaPlayer>namespace Ui {class Widget;}class Widget : public QWidget{ Q_OBJECTpublic: explicit Widget(QWidget *parent = 0); ~Widget();private slots: void on_pushButton_clicked();private: Ui::Widget *ui; QMediaPlayer *player;};#endif // WIDGET_H
widget.cpp
#include "widget.h"#include "ui_widget.h"#include<QDebug>Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget){ ui->setupUi(this); player=new QMediaPlayer(this);}Widget::~Widget(){ delete ui;}void Widget::on_pushButton_clicked(){ QByteArray url="http://tsn.baidu.com/text2audio?"; url.append(QString("&lan=zh&cuid=***&ctp=1&tok=***&pit=8&per=3")); url.append("&tex="); url.append(QUrl::toPercentEncoding(ui->textEdit->toPlainText())); qDebug()<<url; player->setMedia(QUrl::fromLocalFile(url)); player->play();}
Source: tombs
Http://www.cnblogs.com/qflyue/p/6964988.html