首先,我們測試一下Google TTS英文文本朗讀
命令列測試:
wget -q -U Mozilla -O "helloworld.mp3" "http://translate.google.com/translate_tts?ie=UTF-8&tl=en&q=hello+world"
然後我們編寫一段Qt代碼,測試中文語音朗讀,下載後的檔案名稱叫 translate_tts,在手機該應用的data目錄,比如
/accounts/1000/appdata/com.example.Hello1Standard.testDev_lo1Standard73e1edad/data
我們可以把這個mp3檔案拷貝到BlackBerry 10的Windows網盤上面,然後把手機USB串連到PC機上,在PC機上播放。也可以在手機上用“音樂”程式直接播放。
cp translate_tts.mp3 /accounts/1000/shared/music/helloworld.mp3
Qt代碼如下:
/** 設定編碼 begin **/QTextCodec *codec = QTextCodec::codecForName("UTF-8");QTextCodec::setCodecForLocale(codec);QTextCodec::setCodecForCStrings(codec);QTextCodec::setCodecForTr(codec);/** 設定編碼 end **/ DownloadManager* downloader = new DownloadManager(&app); QString url = "http://translate.google.com/translate_tts?&tl=zh-CN&q=北京歡迎你"; QUrl qurl = QUrl::fromEncoded(url.toLocal8Bit()); downloader->SingleDownload(qurl);
DownloadManager.h標頭檔
#ifndef DOWNLOADMANAGER_H#define DOWNLOADMANAGER_H#include <QFile>#include <QObject>#include <QQueue>#include <QTime>#include <QUrl>#include <QtNetwork/QNetworkAccessManager>class DownloadManager: public QObject{ Q_OBJECTpublic: 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檔案
#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::DownloadManager(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 10 app, 使用相對目錄 QDir downloadDir(modDir); QString path = url.path(); QString basename = modDir + "/" + QFileInfo(path).fileName(); //如果下載url沒有檔案名稱,或者檔案名稱尾碼,則取名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 successfully\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(), qPrintable(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::downloadProgress(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::downloadFinished(){ //progressBar.clear(); output.close(); if (currentDownload->error()) { // download failed fprintf(stderr, "Failed: %s\n", qPrintable(currentDownload->errorString())); } else { printf("Succeeded.\n"); ++downloadedCount; } qDebug() << "output file name " << output.fileName(); currentDownloadSaveFileName = output.fileName(); qWarning("downloadFinished"); finished(); currentDownload->deleteLater(); startNextDownload();}void DownloadManager::downloadReadyRead(){qWarning("downloadReadyRead"); output.write(currentDownload->readAll());}
參考:
google text-to-speech API及參考資料
http://hi.baidu.com/sjbh_blog/item/10844d3be0676ac5382ffa5e
downloadmanager應該是Nokia官方的例子代碼,可以google一下 qt downloadmanager.h