QT calls Baidu Voice Rest API for speech synthesis

Source: Internet
Author: User

QT calls Baidu Voice Rest API for speech synthesis

1, first click on the link Http://yuyin.baidu.com/docs/tts

Click Access_token, Get Access_token, there are detailed steps, no longer repeat

Make a note of the link, which will be used in the QT program, after Tex with the text to be converted to speech, Tok back is just obtained access_token

2, open Qt Creator, create a new Qwidget application, draw the interface as follows

3. Get Voice button slot function as follows

voidwidget::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 ();}

The player is an object of the Qmediaplayer class and has a declaration in the Widget.h

Private
Qmediaplayer *player;
Add the following code to the constructor:
Player=new Qmediaplayer (this);
Replace the cuid with your own computer MAC address, Tok and the first step to get the Access_token
Here is all the code
Widget.h
#ifndef Widget_h#defineWidget_h#include<QWidget>#include<QMediaPlayer>namespaceUi {classWidget;}classWidgets: Publicqwidget{Q_object Public:    ExplicitWidget (Qwidget *parent =0); ~widgets ();PrivateSlots:voidon_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 (Newui::widget) {UI-&GT;SETUPUI ( This); Player=NewQmediaplayer ( This);} Widget::~Widget () {DeleteUI;}voidwidget::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: Tomb School

Http://www.cnblogs.com/qflyue/p/6964988.html



QT calls Baidu Voice Rest API for speech synthesis

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.