QMediaPlayer not looping
-
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! -
@tjktak1002 said in QMediaPlayer not looping:
music->setLoops(QMediaPlayer::Infinite);
Did you try to put it before
music->play();
?
-
@jsulm I have already tried that but it doesn't seem to work
-
@tjktak1002 Maybe a bug in Qt. You can search on Qt bug tracker.
-
@tjktak1002
I know nothing about this, but https://stackoverflow.com/questions/37690616/play-background-music-in-a-loop-qtSounds 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 yourmusic->setLoops(QMediaPlayer::Infinite);
? -
@JonB thanks for your answer, I will give it a try
-
-
I have the same problem on Ubuntu 22.04 with Qt 6.4.2. I have (re)opened a bug report.
-
-
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.)
-
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(); }
Post 1 of 9