(char*)buffer.constData(),buffer.byteCount()
void MainWindow::reply_readyRead(){ QByteArray line(reply->readLine()); buf.write(line); }
buf.seek(0); player->setMedia(QMediaContent(),&buf); player->setVolume(55); player->play(); qDebug() << "audio avail" << player->isAudioAvailable(); // return false qDebug() << player->errorString(); // return "*/"
buff.seek(0); QFile file("/tmp/file.mp3"); file.open(QIODevice::WriteOnly | QFile::Truncate); qDebug() << buff.size(); file.write(buff.data(),buff.size()); file.close();
buff.write(reply->readLine())
import requests,io,urllib2url="http://mp3.stream.tb-group.fm/tb.mp3?"response = urllib2.urlopen(url)CHUNK = 16 * 1024with open("out_.mp3","wb") as f: while True: try: chunk = response.read(CHUNK) if not chunk: break f.write(chunk) except KeyboardInterrupt: f.close() break
C++ (Qt)#include <QCoreApplication>#include <QNetworkAccessManager> #include <QFile>#include <QDebug>#include <QThread>#include <QNetworkReply>#include <QNetworkRequest> class KeyboaradHandler : public QObject{ Q_OBJECTpublic: Q_INVOKABLE void start() { if (QThread::currentThread() != thread()) { QMetaObject::invokeMethod(this, "start", Qt::QueuedConnection); return; } getchar(); emit done(); } signals: void done();}; class Handler : public QObject{ Q_OBJECTpublic: Handler() : mKeyboard(new KeyboaradHandler()), mFile(nullptr) { connect(mKeyboard, &KeyboaradHandler::done, this, &Handler::onDone, Qt::QueuedConnection); QNetworkRequest req(QUrl::fromUserInput("http://mp3.stream.tb-group.fm/tb.mp3?")); QNetworkReply *reply = mNetworkManager.get(req); connect(reply, &QNetworkReply::readyRead, this, &Handler::onReadyRead); } ~Handler() { if (mFile) { mFile->close(); delete mFile; } mKeyboard->deleteLater(); mThread.quit(); mThread.wait(); } void onDone() { qDebug() << "finish...."; qApp->quit(); } void onReadyRead() { if (!mFile) { mFile = new QFile("test.mp3"); if (!mFile->open(QIODevice::WriteOnly)) { qDebug() << "unable to open file"; qApp->quit(); return; } } else if (!mThread.isRunning()) { qDebug() << "let's go!"; startWaitAnyKey(); } QNetworkReply *r = qobject_cast<QNetworkReply*>(sender()); mFile->write(r->readAll()); } void startWaitAnyKey() { mThread.start(); mKeyboard->moveToThread(&mThread); mKeyboard->start(); } signals: private: QNetworkAccessManager mNetworkManager; QThread mThread; KeyboaradHandler *mKeyboard; QFile *mFile;}; int main(int argc, char *argv[]){ QCoreApplication a(argc, argv); Handler h; return a.exec();} #include "main.moc"
C++ (Qt)while(!asleep()) sheep++;