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

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.
  • T Offline
    T Offline
    tjktak1002
    wrote on 30 Jun 2022, 07:43 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!

    J 1 Reply Last reply 30 Jun 2022, 07:47
    0
    • T tjktak1002
      30 Jun 2022, 07:43

      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!

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 30 Jun 2022, 07:47 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

      T 1 Reply Last reply 30 Jun 2022, 07:58
      0
      • J jsulm
        30 Jun 2022, 07:47

        @tjktak1002 said in QMediaPlayer not looping:

        music->setLoops(QMediaPlayer::Infinite);

        Did you try to put it before

        music->play();
        

        ?

        T Offline
        T Offline
        tjktak1002
        wrote on 30 Jun 2022, 07:58 last edited by
        #3

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

        J J 2 Replies Last reply 30 Jun 2022, 07:59
        0
        • T tjktak1002
          30 Jun 2022, 07:58

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

          J Offline
          J Offline
          jsulm
          Lifetime Qt Champion
          wrote on 30 Jun 2022, 07:59 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 12 Jan 2023, 14:49
          0
          • T tjktak1002
            30 Jun 2022, 07:58

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

            J Offline
            J Offline
            JonB
            wrote on 30 Jun 2022, 09:04 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);?

            T 1 Reply Last reply 30 Jun 2022, 19:07
            0
            • J JonB
              30 Jun 2022, 09:04

              @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);?

              T Offline
              T Offline
              tjktak1002
              wrote on 30 Jun 2022, 19:07 last edited by
              #6

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

              1 Reply Last reply
              0
              • J jsulm
                30 Jun 2022, 07:59

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

                O Offline
                O Offline
                odelaune
                wrote on 12 Jan 2023, 14:49 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 13 Jan 2024, 05:37 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 13 Jan 2024, 06:24 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