Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMediaPlayer not playing from IODevice
QtWS25 Last Chance

QMediaPlayer not playing from IODevice

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmediaplayerqbufferqbytearraystreamingqiodevice
16 Posts 8 Posters 10.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O onek24
    30 May 2016, 08:32

    Hello,

    i am trying to read a video/audio file into a QByteArray and then pass it to a QMediaPlayer so it will perform on that stream. I am using the following code-snippet:

    	QFile file("example.mp4");
    	file.open(QIODevice::ReadOnly);
    	QByteArray *ba = new QByteArray();
    	ba->append(file.readAll());
    	QBuffer *buffer = new QBuffer(ba);
    	qDebug() << "Buffer size:" << buffer->size(); // looks like it loaded the file
    	player->setMedia(QMediaContent(), buffer);
    	player->play();
    

    where player is from type QMediaPlayer.

    I'm not sure why it doesn't work. Maybe the problem is in the reading of the file?
    Thanks in advance.

    Edit:
    I am able to play the video if i pass it with QUrl::fromLocalFile so i am able to play the video at all, but not from the buffer.

    R Offline
    R Offline
    raven-worx
    Moderators
    wrote on 30 May 2016, 11:57 last edited by raven-worx
    #2

    @onek24
    connect to QMediaPlayer's mediaStatusChanged signal and check the reason there. Maybe also check what error() returns, it might already give a clue.

    Another thing which comes to may head is that you need to open the device (QBuffer) before you set it to the media player instance.

    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
    If you have a question please use the forum so others can benefit from the solution in the future

    1 Reply Last reply
    0
    • O Offline
      O Offline
      onek24
      wrote on 30 May 2016, 12:07 last edited by onek24
      #3

      @raven-worx

      I already tried it that way:

      QObject::connect(player, &QMediaPlayer::mediaStatusChanged, [=](){
      	qDebug() << "Media status changed:" << player->mediaStatus();
      });
      QObject::connect(player, static_cast<void(QMediaPlayer::*)(QMediaPlayer::Error)>(&QMediaPlayer::error),
      	    [=](QMediaPlayer::Error error) {
      	qDebug() << "Error occured:" << player->error();
      });
      QFile file("example.mp4");
      file.open(QIODevice::ReadWrite);
      QByteArray *ba = new QByteArray();
      ba->append(file.readAll());
      QBuffer *buffer = new QBuffer(ba);
      qDebug() << "Buffer size:" << buffer->size();
      player->setMedia(QMediaContent(), buffer);
      player->play();
      

      I get the following output:

      Buffer size: 85221984
      Error occured: QMediaPlayer::ResourceError
      Media status changed: QMediaPlayer::InvalidMedia
      Error occured: QMediaPlayer::ResourceError
      

      When i open the buffer before i set it as a media stream, i get the following output:

      Buffer size: 85221984
      Media status changed: QMediaPlayer::LoadingMedia
      Media status changed: QMediaPlayer::BufferedMedia
      

      With the open buffer, the output looks exactly like when i have an file which the player managed to play. Altho the player isnt able to play it. Since the media player can play the resource from local file using QUrl, maybe the problem is how i read the file into the buffer?

      Edit:

      Also tried opening the buffer and writing into the Buffer directly. Also tried it with seek(0) before the write(no reason to do so, but whatever..) and after the write. Also tried it with closing the buffer after i have written into the buffer.

      It also seems like the mediaplayer cant get any information for the file. The duration is 0, the resolution is [-1, -1].

      1 Reply Last reply
      0
      • O Offline
        O Offline
        onek24
        wrote on 31 May 2016, 08:17 last edited by onek24
        #4

        Aditional Information:

        • Windows 7 Professional 64 bit
        • Qt 5.6.0
        • MSVC 2013, 32 bit

        Update:

        I also tried this and this doesn't seem to work too. No errors are set but still my duration is 0.

        QMediaPlayer* player = new QMediaPlayer(nullptr, QMediaPlayer::StreamPlayback);
        
        QFile* file = new QFile("example.mp4");
        if (file->open(QFile::ReadOnly)) {
            player->setMedia(QMediaContent(), file);
            player->play();
        }
        else {
            qDebug() << "Unable to open file.";
        }
        
        1 Reply Last reply
        0
        • K Offline
          K Offline
          kuzulis
          Qt Champions 2020
          wrote on 31 May 2016, 09:06 last edited by
          #5

          You need to install a codecs-pack.. e.g. K-Lite .. or other..

          O 1 Reply Last reply 31 May 2016, 09:25
          0
          • K kuzulis
            31 May 2016, 09:06

            You need to install a codecs-pack.. e.g. K-Lite .. or other..

            O Offline
            O Offline
            onek24
            wrote on 31 May 2016, 09:25 last edited by
            #6

            @kuzulis
            How is that so? Since the video is playing fine(without a stream), why do i need a codec pack?

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kuzulis
              Qt Champions 2020
              wrote on 31 May 2016, 10:35 last edited by
              #7

              I don't know, it is just my suggestion... Maybe the reason is that in case of a stream, the QMediaPlayer do not know the format of a video... because it does not read a video header of a file... :)

              O 1 Reply Last reply 31 May 2016, 10:48
              0
              • K kuzulis
                31 May 2016, 10:35

                I don't know, it is just my suggestion... Maybe the reason is that in case of a stream, the QMediaPlayer do not know the format of a video... because it does not read a video header of a file... :)

                O Offline
                O Offline
                onek24
                wrote on 31 May 2016, 10:48 last edited by onek24
                #8

                @kuzulis
                Well, thank you anyway. Sadly installing a codec pack didn't fix the problem.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 31 May 2016, 21:59 last edited by
                  #9

                  Hi,

                  Checking the doc of setMedia:

                  Setting the media to a null QMediaContent will cause the player to discard all information relating to the current media source and to cease all I/O operations related to that media.
                  

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  O 1 Reply Last reply 1 Jun 2016, 21:35
                  0
                  • SGaistS SGaist
                    31 May 2016, 21:59

                    Hi,

                    Checking the doc of setMedia:

                    Setting the media to a null QMediaContent will cause the player to discard all information relating to the current media source and to cease all I/O operations related to that media.
                    
                    O Offline
                    O Offline
                    onek24
                    wrote on 1 Jun 2016, 21:35 last edited by onek24 6 Jan 2016, 21:42
                    #10

                    @SGaist

                    Thanks, but i have already tried creating a QMediaContent from the video file and passing it to my mediaplayer with the buffer. That didn't work either.

                    Maybe im understanding it wrong. Maybe the stream has to be actual video or audio data instead of a container like mp4 or mkv? And the QMediaContent the actual information for either video or audio data, instead of the full container?

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      onek24
                      wrote on 2 Jun 2016, 09:15 last edited by onek24 6 Feb 2016, 09:17
                      #11

                      Since i was still unable to solve my problem:

                      Could someone please provide me a working block of code of a QMediaPlayer playing from a buffer or other IODevice? I would like to provide media-data while the QMediaPlayer plays. QTemporaryFile or something similar isn't an option too since it has to be live.

                      So basically: Playing from an IODevice while the device provides more data during playtime.

                      Thanks in advance.

                      M 1 Reply Last reply 5 Jun 2016, 13:00
                      0
                      • O onek24
                        2 Jun 2016, 09:15

                        Since i was still unable to solve my problem:

                        Could someone please provide me a working block of code of a QMediaPlayer playing from a buffer or other IODevice? I would like to provide media-data while the QMediaPlayer plays. QTemporaryFile or something similar isn't an option too since it has to be live.

                        So basically: Playing from an IODevice while the device provides more data during playtime.

                        Thanks in advance.

                        M Offline
                        M Offline
                        MajidKamali
                        wrote on 5 Jun 2016, 13:00 last edited by
                        #12

                        Hi.
                        QTemporaryFile can solve your problem
                        QTemporaryFile is based on QObject, so you can create a pointer to it and set the parent to QApplication:

                        // in main.cpp
                        QTemporaryFile *file = new QTemporaryFile(&app);
                        // use file in application
                        

                        file will be deleted when app destructor is called. (end of application)
                        It is better to create a singleton class derived from QObject that wraps temp file IO
                        use above method for setting its parent as application

                        O 1 Reply Last reply 5 Jun 2016, 13:26
                        -1
                        • M MajidKamali
                          5 Jun 2016, 13:00

                          Hi.
                          QTemporaryFile can solve your problem
                          QTemporaryFile is based on QObject, so you can create a pointer to it and set the parent to QApplication:

                          // in main.cpp
                          QTemporaryFile *file = new QTemporaryFile(&app);
                          // use file in application
                          

                          file will be deleted when app destructor is called. (end of application)
                          It is better to create a singleton class derived from QObject that wraps temp file IO
                          use above method for setting its parent as application

                          O Offline
                          O Offline
                          onek24
                          wrote on 5 Jun 2016, 13:26 last edited by
                          #13

                          @MajidKamali

                          It's no option. I'm trieing to archieve a "straming"-like behaviour.

                          C 1 Reply Last reply 22 Dec 2016, 20:01
                          0
                          • O onek24
                            5 Jun 2016, 13:26

                            @MajidKamali

                            It's no option. I'm trieing to archieve a "straming"-like behaviour.

                            C Offline
                            C Offline
                            chachora
                            wrote on 22 Dec 2016, 20:01 last edited by
                            #14

                            @onek24 Finally did you find the solution?

                            1 Reply Last reply
                            0
                            • H Offline
                              H Offline
                              hariny
                              wrote on 4 May 2017, 14:02 last edited by
                              #15

                              Did anyone solve this?
                              I am trying to decrypt a video before supplying the bytes to qmediaplayer. it doesn't seem to connect to the qiodevice.

                              M 1 Reply Last reply 9 Jan 2018, 15:20
                              1
                              • H hariny
                                4 May 2017, 14:02

                                Did anyone solve this?
                                I am trying to decrypt a video before supplying the bytes to qmediaplayer. it doesn't seem to connect to the qiodevice.

                                M Offline
                                M Offline
                                Mohammad Kanan
                                wrote on 9 Jan 2018, 15:20 last edited by Mohammad Kanan 1 Sept 2018, 15:41
                                #16

                                @hariny , @onek24 Since this thread was not resolved;
                                the buffer is not open !!!
                                your are missing two lines of code:

                                        buffer->open(QIODevice::ReadOnly);
                                        buffer->seek(0);
                                
                                1 Reply Last reply
                                -1

                                • Login

                                • Login or register to search.
                                • First post
                                  Last post
                                0
                                • Categories
                                • Recent
                                • Tags
                                • Popular
                                • Users
                                • Groups
                                • Search
                                • Get Qt Extensions
                                • Unsolved