First, let's test Google tts English text aloud
Command-line testing:
Wget-q-u mozilla-o "Helloworld.mp3" "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+ World
Then we write a section of QT Code, test Chinese speech aloud, download the file name Translate_tts, the mobile phone in the application of the data directory, such as
/accounts/1000/appdata/com.example.hello1standard.testdev_lo1standard73e1edad/data
We can copy this mp3 file to the BlackBerry 10 Windows network disk, and then connect the handset USB to the PC and play on the PC computer. It can also be played directly on the phone using the "Music" program.
CP Translate_tts.mp3/accounts/1000/shared/music/helloworld.mp3
The QT code is as follows:
/** set encoding begin **/
qtextcodec *codec = Qtextcodec::codecforname ("UTF-8");
Qtextcodec::setcodecforlocale (codec);
Qtextcodec::setcodecforcstrings (codec);
QTEXTCODEC::SETCODECFORTR (codec);
/** sets the encoding end **/
downloadmanager* downloader = new Downloadmanager (&app);
QString url = "http://translate.google.com/translate_tts?&tl=zh-CN&q= Beijing welcomes You";
Qurl Qurl = qurl::fromencoded (Url.tolocal8bit ());
Downloader->singledownload (Qurl);
DownloadManager.h header File
#ifndef downloadmanager_h #define Downloadmanager_h #include <QFile> #include <QObject> #include <qqueue > #include <QTime> #include <QUrl> #include <QtNetwork/QNetworkAccessManager> class
Downloadmanager:public QObject {q_object Public:downloadmanager (QObject *parent = 0);
void Append (const qurl &url);
void Append (const qstringlist &urllist);
void singledownload (qurl URL);
Signals:void finished ();
void Progress (Qint64 bytesreceived, Qint64 bytestotal, QString message);
Private Slots:void startnextdownload ();
void DownloadProgress (Qint64 bytesreceived, Qint64 bytestotal);
void Downloadfinished ();
void Downloadreadyread ();
Private:qstring savefilename (const qurl &url);
Qnetworkaccessmanager Manager;
Qqueue<qurl> Downloadqueue;
Qnetworkreply *currentdownload;
QString Currentdownloadsavefilename;
Qfile output;
Qtime Downloadtime; int DownloadedcounT
int totalcount;
};
#endif
DownloadManager.cpp file
#include "downloadmanager.h" #include <QFileInfo> #include <QtNetwork/QNetworkRequest> #include < qtnetwork/qnetworkreply> #include <QString> #include <QStringList> #include <QTimer> #include <QDir> #include <stdio.h> #include <QEventLoop> #include <QDebug> downloadmanager::D Ownloadmanager (QObject *parent): QObject (parent), Downloadedcount (0), TotalCount (0) {} void Downloadmanager::append
(const qstringlist &urllist)
{foreach (QString url, urllist) append (qurl::fromencoded (Url.tolocal8bit ()));
if (Downloadqueue.isempty ()) qtimer::singleshot (0, this, SIGNAL (finished ())); } void Downloadmanager::append (const qurl &url) {if (Downloadqueue.isempty ()) qtimer::singleshot (0, this
, SLOT (Startnextdownload ()));
Downloadqueue.enqueue (URL);
++totalcount; } QString downloadmanager::savefilename (const qurl &url) {QString Moddir = "Data";//data is for BlackBerry 10App, using relative directory Qdir downloaddir (moddir);
QString path = Url.path ();
QString basename = Moddir + "/" + qfileinfo (path). FileName ();
If the download URL does not have a filename, or a filename suffix, the name download if (Basename.isempty ()) basename = "Download";
if (qfile::exists (basename)) {//already exists, don ' t overwrite int i = 0;
basename + = '. ';
while (qfile::exists (basename + qstring::number (i))) ++i;
basename + + qstring::number (i);
return basename; } void Downloadmanager::startnextdownload () {if (Downloadqueue.isempty ()) {printf ("%d/%d files downloaded s
Uccessfully\n ", Downloadedcount, TotalCount);
Emit finished ();
Return
} qurl url = downloadqueue.dequeue ();
QString filename = savefilename (URL);
Qwarning ("Downloading to:%s", Filename.tolocal8bit (). data ());
Output.setfilename (filename); if (!output.open (qiodevice::writeonly)) {fprintf (stderr, Problem opening save File '%s ' for download '%s ':%s\n ', qprintable (filename), url.toencoded (). Constdata (), QP
Rintable (Output.errorstring ()));
Startnextdownload (); Return
Skip this download} qnetworkrequest request (URL);
Currentdownload = Manager.get (request);
Connect (Currentdownload, SIGNAL (DownloadProgress (Qint64,qint64)), SLOT (DownloadProgress (Qint64,qint64));
Connect (Currentdownload, SIGNAL (finished ()), SLOT (downloadfinished ());
Connect (Currentdownload, SIGNAL (Readyread ()), SLOT (Downloadreadyread ());
Prepare the output printf ("Downloading%s...\n", url.toencoded (). Constdata ());
Downloadtime.start ();
} void Downloadmanager::singledownload (qurl url) {QString filename = savefilename (URL) + ". mp3";
Qwarning ("Downloading to:%s", Filename.tolocal8bit (). data ());
Qwarning ("Downloading from:%s", Url.path (). Tolocal8bit (). data ()); Output.setfileName (filename); if (!output.open (qiodevice::writeonly)) {fprintf (stderr, Problem opening save file '%s ' for download '%s ':%s\n "
, qprintable (filename), url.toencoded (), Constdata (), Qprintable (Output.errorstring ()));
Startnextdownload (); Return
Skip this download} qwarning ("beginning download");
Qnetworkrequest request;
Request.seturl (URL);
Currentdownload = Manager.get (request);
Connect (Currentdownload, SIGNAL (DownloadProgress (Qint64,qint64)), SLOT (DownloadProgress (Qint64,qint64));
Connect (Currentdownload, SIGNAL (finished ()), SLOT (downloadfinished ());
Connect (Currentdownload, SIGNAL (Readyread ()), SLOT (Downloadreadyread ());
Currentdownload->starttimer (1000);
Currentdownload->open (qiodevice::readonly);
Currentdownload->request ();
Downloadtime.start (); } void Downloadmanager::d ownloadprogress (Qint64 bytesreceived, QiNt64 bytestotal) {//progressbar.setstatus (bytesreceived, bytestotal);
Calculate the download speed double speed = bytesreceived * 1000.0/downloadtime.elapsed ();
QString Unit;
if (Speed < 1024) {unit = "bytes/sec";
else if (Speed < 1024*1024) {speed/= 1024;
Unit = "KB/S";
else {speed/= 1024*1024;
Unit = "MB/s";
} QString spd = Qstring::number (speed);
Progress (bytesreceived, bytestotal, "Downloading at:" + SPD + unit);
SPD = "Downloading at:" + spd + unit;
Qdebug () << "Downloading at:" << spd + "" + Unit;
Qdebug () << bytesreceived << "Bytes Received";
Qdebug () << bytestotal + "bytes total.";
Progressbar.setmessage (qstring::fromlatin1 ("%1%2")//.arg (Speed, 3, ' F ', 1). ARG (unit));
Progressbar.update ();
} void Downloadmanager::d ownloadfinished () {//progressbar.clear ();
Output.close (); if (Currentdownload->error ()) {//download failed fprintf (stderr, "failed:%s\n", qprintable (current
Download->errorstring ()));
else {printf ("succeeded.\n");
++downloadedcount;
} qdebug () << "Output file name" << output.filename ();
Currentdownloadsavefilename = Output.filename ();
Qwarning ("downloadfinished");
Finished ();
Currentdownload->deletelater ();
Startnextdownload ();
} void Downloadmanager::d ownloadreadyread () {qwarning ("Downloadreadyread");
Output.write (Currentdownload->readall ());
}
Reference:
Google Text-to-Speech API and reference
http://hi.baidu.com/sjbh_blog/item/10844d3be0676ac5382ffa5e
Downloadmanager should be Nokia's official example code, you can google the QT downloadmanager.h