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->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