PDA

View Full Version : QMediaPlayer crash using QPlaylist



MongKong
31st March 2020, 19:50
Hello,

so i want to run background music on loop and i searched for it on how to do it on google and i found this bit of code:


playlist = new QMediaPlaylist();
playlist->addMedia(QUrl("qrc:/World_Sounds/BackgroundSound.wav"));
playlist->setPlaybackMode(QMediaPlaylist::Loop);

music = new QMediaPlayer();
music->setPlaylist(playlist);
music->play();


but this code crashes as soon as the first song stops playing ? (when it wants to loop)

but doing only this works, but the problem is it won't loop the media:


music = new QMediaPlayer();
music->setMedia(QUrl("qrc:/World_Sounds/BackgroundSound.wav"));
music->play();


Here is the error message:
13370

More error messages:
https://imgur.com/P0c8UKh

If anyone knows why does this happen, please help :(
Any help is appreciated !!!!

thebestshade
18th November 2020, 16:49
Hello,

I faced the same issue recently using Qt5.12.9, before that the code you provided worked fine.
What I actually did is... I gave up from using QMediaPlaylist and used QMediaPlayer::stateChanged signal instead.

In your case it should be something like:


music = new QMediaPlayer();
music->setMedia(QUrl("qrc:/World_Sounds/BackgroundSound.wav"));

connect(music, &QMediaPlayer::stateChanged, [this](QMediaPlayer::State state) {
if (state == QMediaPlayer::State::StoppedState)
{
music->play();
}
});

music->play();


Hope it may help.