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 looping
QtWS25 Last Chance

QMediaPlayer not looping

Scheduled Pinned Locked Moved Unsolved General and Desktop
qmediaplayerqaudiooutput
9 Posts 5 Posters 1.8k 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.
  • tjktak1002T Offline
    tjktak1002T Offline
    tjktak1002
    wrote on last edited by
    #1

    Hello everyone,
    I have been trying to implemented background music to my software so I need to loop my background music infinitely.
    This is my code and I would like to know why the music does not loop:

    QMediaPlayer *music = new QMediaPlayer();
        QAudioOutput *aO = new QAudioOutput;
        music->setAudioOutput(aO);
        music->setSource(QUrl("qrc:/sounds/music.mp3"));
    
        music->play();
        
        music->setLoops(QMediaPlayer::Infinite);
    

    My current solution for now is using signal and slot instead but I would still prefer understanding the problem with my former codes.
    Thank you and have a good day!

    jsulmJ 1 Reply Last reply
    0
    • tjktak1002T tjktak1002

      Hello everyone,
      I have been trying to implemented background music to my software so I need to loop my background music infinitely.
      This is my code and I would like to know why the music does not loop:

      QMediaPlayer *music = new QMediaPlayer();
          QAudioOutput *aO = new QAudioOutput;
          music->setAudioOutput(aO);
          music->setSource(QUrl("qrc:/sounds/music.mp3"));
      
          music->play();
          
          music->setLoops(QMediaPlayer::Infinite);
      

      My current solution for now is using signal and slot instead but I would still prefer understanding the problem with my former codes.
      Thank you and have a good day!

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

      @tjktak1002 said in QMediaPlayer not looping:

      music->setLoops(QMediaPlayer::Infinite);

      Did you try to put it before

      music->play();
      

      ?

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

      tjktak1002T 1 Reply Last reply
      0
      • jsulmJ jsulm

        @tjktak1002 said in QMediaPlayer not looping:

        music->setLoops(QMediaPlayer::Infinite);

        Did you try to put it before

        music->play();
        

        ?

        tjktak1002T Offline
        tjktak1002T Offline
        tjktak1002
        wrote on last edited by
        #3

        @jsulm I have already tried that but it doesn't seem to work

        jsulmJ JonBJ 2 Replies Last reply
        0
        • tjktak1002T tjktak1002

          @jsulm I have already tried that but it doesn't seem to work

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @tjktak1002 Maybe a bug in Qt. You can search on Qt bug tracker.

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

          O 1 Reply Last reply
          0
          • tjktak1002T tjktak1002

            @jsulm I have already tried that but it doesn't seem to work

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @tjktak1002
            I know nothing about this, but https://stackoverflow.com/questions/37690616/play-background-music-in-a-loop-qt

            Sounds like what you want is QMediaPlaylist. QMediaPlaylist allows you to control the playback mode, and in this case you would use Loop.

            suggests QMediaPlaylist *playlist = new QMediaPlaylist(); playlist->setPlaybackMode(QMediaPlaylist::Loop); rather than your music->setLoops(QMediaPlayer::Infinite);?

            tjktak1002T 1 Reply Last reply
            0
            • JonBJ JonB

              @tjktak1002
              I know nothing about this, but https://stackoverflow.com/questions/37690616/play-background-music-in-a-loop-qt

              Sounds like what you want is QMediaPlaylist. QMediaPlaylist allows you to control the playback mode, and in this case you would use Loop.

              suggests QMediaPlaylist *playlist = new QMediaPlaylist(); playlist->setPlaybackMode(QMediaPlaylist::Loop); rather than your music->setLoops(QMediaPlayer::Infinite);?

              tjktak1002T Offline
              tjktak1002T Offline
              tjktak1002
              wrote on last edited by
              #6

              @JonB thanks for your answer, I will give it a try

              1 Reply Last reply
              0
              • jsulmJ jsulm

                @tjktak1002 Maybe a bug in Qt. You can search on Qt bug tracker.

                O Offline
                O Offline
                odelaune
                wrote on last edited by
                #7

                I have the same problem on Ubuntu 22.04 with Qt 6.4.2. I have (re)opened a bug report.

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  CompSciDude
                  wrote on last edited by
                  #8

                  I have the same issue. I reported a bug on Qt's Bug Tracker, but they have not done anything with it (https://bugreports.qt.io/browse/QTBUG-102360). There is another issue that says it was fixed in 6.5.0 (https://bugreports.qt.io/browse/QTBUG-110113), but I am stuck on 6.2.4.

                  Some more insight on my problem: I am writing a cross-platform application that plays a sound on loop among other things. The sound loops fine on macOS, but it does not loop consistently on Ubuntu 22.04 or Windows. It either plays once and then stops or plays it a number of times, but then stops unexpectedly.

                  I attempted to fix the problem by looping it manually using the playbackStateChanged signal when the QMediaPlayer goes to the StoppedState and then invoking play. This does not seem to fix the problem on Windows. (I have yet to try it on Ubuntu 22.04.)

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    CompSciDude
                    wrote on last edited by
                    #9

                    Ok, I found a solution to my problem. This is the PlaySound method from my SoundEffect class. The area of interest is the code below the note.

                    void SoundEffect::PlaySound() {
                      mPlayer.setSource(soundUrl);
                      // Note about QMediaPlayer::Loops
                      //   Using QMediaPlayer::Loops::Infinite does not work correctly on Linux and Windows.
                      //   It either plays once and then stops or plays multiple times, but stops unexpectedly.
                      //   Therefore, use QMediaPlayer::Loops::Once and connect to the playbackStateChanged signal.
                      //   The slot will reset the QMediaPlayer and play the sound again.
                      mPlayer.setLoops(QMediaPlayer::Loops::Once);
                      connect(&mPlayer, &QMediaPlayer::playbackStateChanged,
                        this, [this, soundUrl](QMediaPlayer::PlaybackState aState) {
                          if (aState == QMediaPlayer::PlaybackState::StoppedState) {
                            // Clear the source
                            mPlayer.setSource(QUrl());
                            // Restore the source
                            mPlayer.setSource(soundUrl);
                            mPlayer.play();
                          }
                        });
                      mPlayer.play();
                    }
                    
                    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