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 mp3 playback stalled
Forum Update on Monday, May 27th 2025

QMediaPlayer mp3 playback stalled

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 164 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.
  • N Offline
    N Offline
    nova_xo
    wrote last edited by
    #1

    Hey there! I'm using QMediaPlayer to play audio files in my app. The issue is that when playing an mp3 file, the playback never starts, but when I manually seek it to change the position, only then does it start playing. There is no such problem with other formats such as FLAC/AAC/ALAC. Here's the code I'm using:

    void player::init(queuemodel *model)
    {
        mediaplayer = new QMediaPlayer(this);
        output = new QAudioOutput(this);
        connect(mediaplayer, &QMediaPlayer::playbackStateChanged, this, &player::player_state_changed);
        connect(mediaplayer, &QMediaPlayer::positionChanged, this, &player::time_changed);
        mediaplayer->setAudioOutput(output);
        output->setVolume(100);
    
        this->model = model;
        reader = new Tag_reader(this);
    
    
        //media status signal
        connect(mediaplayer, &QMediaPlayer::mediaStatusChanged, this, &player::media_status_changed);
    
        emit player_ready();
    }
    
    
    void player::play_current()
    {
    
        QModelIndex index = model->index(current_index, 0);
        mediaplayer->setSource(QUrl::fromLocalFile(model->data(index, queuemodel::PathRole).toString()));
        mediaplayer->play();
    }
    

    Even when i wait for the loaded media signal and then play it, it makes no difference. I'm on Qt6.9 on Windows 11 and using it with QtQuick/QML. Any help is appreciated.

    jsulmJ 1 Reply Last reply
    0
    • N nova_xo

      Hey there! I'm using QMediaPlayer to play audio files in my app. The issue is that when playing an mp3 file, the playback never starts, but when I manually seek it to change the position, only then does it start playing. There is no such problem with other formats such as FLAC/AAC/ALAC. Here's the code I'm using:

      void player::init(queuemodel *model)
      {
          mediaplayer = new QMediaPlayer(this);
          output = new QAudioOutput(this);
          connect(mediaplayer, &QMediaPlayer::playbackStateChanged, this, &player::player_state_changed);
          connect(mediaplayer, &QMediaPlayer::positionChanged, this, &player::time_changed);
          mediaplayer->setAudioOutput(output);
          output->setVolume(100);
      
          this->model = model;
          reader = new Tag_reader(this);
      
      
          //media status signal
          connect(mediaplayer, &QMediaPlayer::mediaStatusChanged, this, &player::media_status_changed);
      
          emit player_ready();
      }
      
      
      void player::play_current()
      {
      
          QModelIndex index = model->index(current_index, 0);
          mediaplayer->setSource(QUrl::fromLocalFile(model->data(index, queuemodel::PathRole).toString()));
          mediaplayer->play();
      }
      

      Even when i wait for the loaded media signal and then play it, it makes no difference. I'm on Qt6.9 on Windows 11 and using it with QtQuick/QML. Any help is appreciated.

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote last edited by
      #2

      @nova_xo said in QMediaPlayer mp3 playback stalled:

      mediaplayer = new QMediaPlayer(this);

      Do you delete the previous player somewhere?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      N 1 Reply Last reply
      0
      • jsulmJ jsulm

        @nova_xo said in QMediaPlayer mp3 playback stalled:

        mediaplayer = new QMediaPlayer(this);

        Do you delete the previous player somewhere?

        N Offline
        N Offline
        nova_xo
        wrote last edited by nova_xo
        #3

        @jsulm No, I do not. The mediaplayer object is defined in the global scope and initialized once the init function is called. The problem is specifically with mp3 files, other formats work fine. Also, I've created only one player.

        1 Reply Last reply
        0
        • N Offline
          N Offline
          nova_xo
          wrote last edited by
          #4

          I found a hack. Though I'm still not sure whats causing the playback to get stalled.

          void player::play_current()
          {
          
              QModelIndex index = model->index(current_index, 0);
              mediaplayer->setSource(QUrl::fromLocalFile(model->data(index, songmodel::PathRole).toString()));
          
              reader->read(model->data(index, songmodel::PathRole).toString().toUtf8().data());
              reader->get_synced_lyrics(model->data(index, songmodel::PathRole).toString().toUtf8().data());
          
              if (model->data(index, songmodel::PathRole).toString().endsWith(".mp3"))
              {
                  mediaplayer->play();
                  wait(1000);
                  set_position(1);
              }else {
                  mediaplayer->play();
              }
          }
          
          void player::wait(int milliseconds)
          {
              QEventLoop loop;
              QTimer::singleShot(milliseconds, &loop, &QEventLoop::quit);
              loop.exec(); // Blocks here until the timer calls quit()
          }
          

          This works for the time being.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            nova_xo
            wrote last edited by
            #5

            250ms timer is the sweet spot for me

            JoeCFDJ 1 Reply Last reply
            0
            • N nova_xo

              250ms timer is the sweet spot for me

              JoeCFDJ Online
              JoeCFDJ Online
              JoeCFD
              wrote last edited by
              #6

              @nova_xo Can you try to set the position first and then play? No timer in this test.

              N 1 Reply Last reply
              0
              • JoeCFDJ JoeCFD

                @nova_xo Can you try to set the position first and then play? No timer in this test.

                N Offline
                N Offline
                nova_xo
                wrote last edited by
                #7

                @JoeCFD I tried it, sadly it doesn't work.

                JoeCFDJ 1 Reply Last reply
                0
                • N nova_xo

                  @JoeCFD I tried it, sadly it doesn't work.

                  JoeCFDJ Online
                  JoeCFDJ Online
                  JoeCFD
                  wrote last edited by JoeCFD
                  #8

                  @nova_xo I think that is the right order. Adding 250ms or 200ms delay is ok. Sometimes it takes some time for the pipeline to be ready. Nothing wrong with some delay.

                  N 1 Reply Last reply
                  0
                  • JoeCFDJ JoeCFD

                    @nova_xo I think that is the right order. Adding 250ms or 200ms delay is ok. Sometimes it takes some time for the pipeline to be ready. Nothing wrong with some delay.

                    N Offline
                    N Offline
                    nova_xo
                    wrote last edited by
                    #9

                    @JoeCFD What I don't understand is why is this happening specifically with mp3 files. All other formats work fine. What I'm doing is hacky at best and not a concrete solution.

                    JoeCFDJ 1 Reply Last reply
                    0
                    • N nova_xo

                      @JoeCFD What I don't understand is why is this happening specifically with mp3 files. All other formats work fine. What I'm doing is hacky at best and not a concrete solution.

                      JoeCFDJ Online
                      JoeCFDJ Online
                      JoeCFD
                      wrote last edited by
                      #10

                      @nova_xo I am not sure either. But be aware that some delay is often needed in the pipeline for state or position change. Good you found the solution. I use raw gstreamer code and can find out where waiting is exactly needed. You may need to guess a bit with Qt.

                      1 Reply Last reply
                      0

                      • Login

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