QT调用百度语音REST API实现语音合成
1、首先点击点击链接http://yuyin.baidu.com/docs/tts
点击access_token,获取access_token,里面有详细步骤,不再赘述
记下链接,等会在QT程序中会用到,tex后面跟要转换成语音的文字,tok后面是刚获得的access_token
2、打开Qt Creator,新建一个QWidget应用程序,绘制界面如下
3、获取语音按钮槽函数如下
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是QMediaPlayer类的一个对象,在widget.h中有声明
private:
QMediaPlayer *player;
构造函数中添加如下代码:
player=new QMediaPlayer(this);
将cuid换成自己计算机mac地址,tok后加第一步获得的access_token
以下是所有代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H #include <QWidget>
#include <QMediaPlayer>
namespace Ui {
class Widget;
} class Widget : public QWidget
{
Q_OBJECT public:
explicit Widget(QWidget *parent = );
~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();
}
来源:古墓派掌门
http://www.cnblogs.com/qflyue/p/6964988.html