It looks like you want a QMediaPlaylist . QMediaPlaylist allows you to control the playback mode, in which case you should use Loop . This approach has other benefits, such as CurrentItemInLoop . CurrentItemInLoop will play the current playlist item in a loop, which means that if you add more songs in the future, you can select a song and then create only that track. Thus, for most needs, you only need one playlist. Below is an example of the code, currently I donβt have the tools to test it (Qt multimedia extensions are not installed on this computer). The point should be demonstrated quite well, though.
QMediaPlaylist *playlist = new QMediaPlaylist(); playlist->addMedia(QUrl("qrc:/sounds/backgroundmusic.mp3")); playlist->setPlaybackMode(QMediaPlaylist::Loop); QMediaPlayer *music = new QMediaPlayer(); music->setPlaylist(playlist); music->play();
source share