循环播放背景音乐 Qt [英] play background music in a loop Qt

查看:48
本文介绍了循环播放背景音乐 Qt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想循环播放背景音乐直到游戏结束.

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

在头文件中:

    QMediaPlayer * music = new QMediaPlayer();

在 cpp 文件中:

    startGame(){
    music->setMedia(QUrl("qrc:/sounds/backgroundmusic.mp3"));
    music->play();  }

   stopGame(){
   music->stop(); }

现在我的音乐一直播放到最后,但没有重新启动.我怎样才能让它再次循环?是否有我可以使用的 QMediaPlayer 成员,或者我应该在 while 循环中运行它,还是什么?

Right now my music plays thru to the end but does not restart. How can I get it to loop over again again? Is there a QMediaPlayer member I can use, or should I run it in a while loop, or what?

推荐答案

听起来你想要的是 QMediaPlaylist.QMediaPlaylist 允许您控制播放模式,在这种情况下,您将使用 Loop.这种方法还有其他优点,例如 CurrentItemInLoop.CurrentItemInLoop 将循环播放当前播放列表项,这意味着如果您将来添加更多歌曲,您可以选择一首歌曲然后仅循环播放该曲目.因此,您只需要一个播放列表即可满足大多数需求.下面是一些示例代码,我目前没有办法测试它(这台机器上没有安装 Qt 多媒体扩展).不过应该可以很好地证明这一点.

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();

这篇关于循环播放背景音乐 Qt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆