1
votes

I use QMediaPlayer to play online mp3 and I want to download the whole mp3 data when buffer is complete. But the only function relate to it is setMedia(const QMediaContent & media, QIODevice * stream = 0).

And the stream pointer is used to put in the data . So I try to use QReply as its arg.And it fails absolutely.

QNetworkRequest request;
request.setUrl((QUrl(source)));
pReply = mgr.get(request);
setMedia(QUrl(),pReply);

The mgr is QNetworkAccessManager and source is the online url of mp3.

My question is : is there any way to get the music data? Is setMedia can do it ?

--------------------------Update----------------

I connect the reply's readReady signal and put it into setMedia at first time that reply have something to read.But the QMediaPlayer return the QMediaPlayer::ResourceError error.

P.S. I added the QMediaPlayer::StreamPlayback flag.

------------------------update--------------------------------------

I found why my code don't work. The setMedia function call DirectShowPlayerService::load inertal. And there is a line check like this:

else if (stream && (!stream->isReadable() || stream->isSequential())) {
    m_pendingTasks = 0;
    m_graphStatus = InvalidMedia;
    m_error = QMediaPlayer::ResourceError;
}

For isSequential() there is doc:

bool QIODevice::isSequential() const [virtual] Returns true if this device is sequential; otherwise returns false.

Sequential devices, as opposed to a random-access devices, have no concept of a start, an end, a size, or a current position, and they do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential.

Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential.

At last , the network reply can't be used in this condition. :-(

So My further research may be : how dose Qt deal with the network socket for this . And is there any way to get the data ?

------------------update----------------------

It seems that QMediaPlayer is based on GStreamer and there is a way to download the data in the source code's comment.But I haven't used the GStreamer , so I have difficult to use the download function with Qt.

And only useful information I found is :QMediaPlayer API for download the playing stream .But I don't know how to get my way . Please help me!

1

1 Answers

0
votes

If you want to download your media and play after, you can't do it this :

pReply = mgr.get(request);

Use the "finish" signal of QNetworkAccessManager. void QNetworkAccessManager::finished ( QNetworkReply * reply )

Don't forget, when you initializing your QMediaPlayer, you should use QMediaPlayer::StreamPlayback

QMediaPlayer* player = new QMediaPlayer(0, QMediaPlayer::StreamPlayback);

And i found this, maybe you have a bug with your Qt version :

Link