QT calls Baidu speech rest api for speech synthesis and rest speech synthesis

Source: Internet
Author: User

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

 

 

 


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.