Playing media from QIODevice and nothing else

Discuss anything related to product development
Post Reply
bankai
Posts: 6
Joined: Sun Jun 26 2022 2:44 pm

Playing media from QIODevice and nothing else

Post by bankai »

I am working on an application that has its music files in Ogg format embedded in its own containers. I can 'open' those files and present them as a QIODevice *. I then want to play them using QMediaPlayer but that simply refuses to play anything that does not have a valid URL, which is a bit counter to what the documentation of QMediaPlayer::setMedia says:
Sets the current media source.

If a stream is supplied, media data will be read from it instead of resolving the media source. In this case the media source may still be used to resolve additional information about the media such as mime type. The stream must be open and readable.
I am setting the MIME type explicitly:

Code: Select all

    QIODevice *m_input = openContainer("1.ogg");
  
    QMediaResource resource (QUrl (), "audio/ogg");
    QMediaContent content (resource);
    qDebug() << "isNull" << content.isNull();  // is false, by the way

    m_player.setMedia (content, m_input);
    m_player.play ();
I have tried may things for the QMediaContent, the QUrl and mime-type, but it will only play audio when QUrl points to something it can open. I tried 'null', no string, empty string, 'file://', et cetera. Typical error message is:

Code: Select all

isNull false
GStreamer; Unable to pause - 
Player::newMediaStatus: 2
GStreamer; Unable to pause - 
Player::newMediaStatus: 8
Error: Invalid URI "".
Often if does not even print this or emit the MediaPlayer mediaStatusChanged signal. So, any suggestions on how to get the QMediaPlayer to behave?
seasoned_geek
Posts: 246
Joined: Thu Jun 11 2020 12:18 pm

Re: Playing media from QIODevice and nothing else

Post by seasoned_geek »

Do you have a _complete_ buildable example you can zip up somewhere? Source, data, cmake, etc.

I have no idea why you are using QIODevice when 1.ogg is a file. Might have some time this weekend or on Monday to try converting your code to use a QFile which can be consumed by a stream. Need to also see what openContainer() is really doing. From the tiny snippets you present it "looks" like openContainer() is operating as a QDir so you shouldn't be getting a device of any kind back.
bankai
Posts: 6
Joined: Sun Jun 26 2022 2:44 pm

Re: Playing media from QIODevice and nothing else

Post by bankai »

seasoned_geek wrote: Sat Jul 09 2022 12:58 pm Do you have a _complete_ buildable example you can zip up somewhere? Source, data, cmake, etc.

I have no idea why you are using QIODevice when 1.ogg is a file.
It is not a regular file. The '1.ogg' is just the internal name, the actual content is somewhere inside the container. That could be a file (with multiple entries like a ZIP file), a compiled-in resource or something I generate on the fly in memory. Hence the QIODevice...
seasoned_geek wrote: Sat Jul 09 2022 12:58 pm Might have some time this weekend or on Monday to try converting your code to use a QFile which can be consumed by a stream. Need to also see what openContainer() is really doing. From the tiny snippets you present it "looks" like openContainer() is operating as a QDir so you shouldn't be getting a device of any kind back.
No QDir involved; again, I don't deal with regular files here; in openContainer() I create a QByteArray, filled with the contents of the .ogg file, then create a QIODevice on top of it. I'll see if I can whip up an example but I have to write it from scratch, it is too much code otherwise.

In any case, a QFIle won't be the solution either when we want to use QMediaPlayer with some other format that is an audio stream but which has no URL, file or anything 'physical' associated with that.
Post Reply