play background music in a loop Qt

前端 未结 1 1026
余生分开走
余生分开走 2020-12-06 18:48

I want to play background music continually in a loop until the game ends.

in the header file:

    QMediaPlayer * music = new QMediaPlayer();


        
相关标签:
1条回答
  • 2020-12-06 18:59

    Sounds like what you want is QMediaPlaylist. QMediaPlaylist allows you to control the playback mode, and in this case you would use Loop. This approach has other advantages too, such as CurrentItemInLoop. CurrentItemInLoop will play the current playlist item in a loop, meaning that if you add more songs in the future you can select a song then loop only that track. Thus, you only need a single playlist for most needs. Below is some example code, I do not currently have a means to test it though (No Qt multimedia extensions installed on this machine). Should demonstrate the point reasonably 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();
    
    0 讨论(0)
提交回复
热议问题