【发布时间】:2018-11-18 17:41:18
【问题描述】:
我需要从使用 Qt 及其 QMediaplayer 类播放的互联网广播流中获取数据,如下所示:
player->setMedia(QMediaContent(QUrl("http://rfm-live-mp3-64.scdn.arkena.com/rfm.mp3")),
但我不知道如何访问流数据并将其记录到缓冲区或文件中。我尝试了 QAUdioRecorder,但可用的 audioIn 似乎只是麦克风。
有谁知道 Qt 是否可以做到这一点?
谢谢!
编辑----------- 这是 eyllanesc 建议的代码:
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QFile file(R"(D:\\Mes_Projets_Qt_Creator\\build-TestQtDesignWidget-
Desktop_Qt_5_11_0_MinGW_32bit-Debug\\ML.wav)");
file.open(QIODevice::ReadOnly);
QByteArray arr = file.readAll();
QBuffer *buffer = new QBuffer(player);
buffer->setData(arr);
buffer->open(QIODevice::ReadOnly);
QFile autreFile("hello.wav");
autreFile.open(QIODevice::WriteOnly);
QDataStream out(&autreFile);
out << arr;
QAudioFormat format;
format.setSampleRate(22050);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info(QAudioDeviceInfo::defaultOutputDevice());
if (!info.isFormatSupported(format)) {
qWarning() << "Raw audio format not supported by backend, cannot play
audio.";
return;
}
QAudioOutput* audioOutput = new QAudioOutput(format, this);
audioOutput->start(buffer);
foreach (const QAudioDeviceInfo &deviceInfo,
QAudioDeviceInfo::availableDevices(QAudio::AudioOutput))
qDebug() << "Device name: " << deviceInfo.deviceName();
QNetworkAccessManager nam;
QNetworkRequest request(QUrl("http://rfm-live-mp3-
64.scdn.arkena.com/rfm.mp3"));
QString downloadDir =
QStandardPaths::writableLocation(QStandardPaths::DownloadLocation);
QFile file2(QDir(downloadDir).absoluteFilePath("test.mp3"));
QDataStream ds(&file2);
if(!file2.open(QFile::WriteOnly))
//return -1;
QNetworkReply *reply = nam.get(request);
QObject::connect(reply, &QNetworkReply::downloadProgress, [reply, &ds]
(qint64 bytesReceived, qint64 bytesTotal){
ds << reply->readAll();
qDebug()<<bytesReceived<<bytesTotal;
});
qDebug()<<reply;
【问题讨论】:
-
我今天尝试设置它,我认为它在插槽中,但我无法正确设置;我刚刚看到您的编辑和详细信息:感谢您花时间制作这个例子。我终于复制粘贴了:效果很好!非常感谢