SFML 在另一首歌曲完成后开始播放新歌曲

SFML starting a new song once another song is complete

#include <iostream>
#include <SFML/Window.hpp>
#include <SFML/Window/Event.hpp>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Audio/Music.hpp>


using namespace std;

int main (){

    sf::RenderWindow window(sf::VideoMode(800, 600), "My Window");

        sf::Music music;
        if (!music.openFromFile("Music/Fallen-Down.ogg"))
            return EXIT_FAILURE;
    music.play();

//  if(!music.openFromFile("Music/CORE.ogg"))
//      return EXIT_FAILURE;

    while (window.isOpen()){
            // check all the window's events that were triggered since the last iteration        of the loop
            sf::Event event;
            while (window.pollEvent(event)) {
             // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                         window.close();                
            }
        
        if(music.getStatus() == 0)
            music.play();
    
        window.clear(sf::Color(255, 255, 255, 0));

        window.display();

        }
    return EXIT_SUCCESS;
}

我有基本的 window,可以打开并播放歌曲。我只想在第一首歌曲“Fallen-Down”播放完后播放下一首歌曲“CORE.ogg”。我尝试使用 SFML Documentation 中的 getStatus() 函数,我相信 returns 一个整数对应于定义为

的枚举中的歌曲状态

enum Status {Stopped, Paused, Playing}

我检查它是哪个号码 returns 如果是 returns 0 我希望它能再次播放。它在 window 循环中,所以它应该在每个循环结束时检查歌曲的状态...

如果你想再次播放音乐,那么你需要stop()当前的音乐,即使它已经结束了,因为这会使音乐倒回开头。

if(music.getStatus() == sf::SoundSource::Status::Stopped)
{
    music.stop(); // rewind to beginning
    music.play();
}

顺便说一句,您应该使用 sf::SoundSource::Status::Stopped 而不是 0,因为它可能会在未来版本或其他平台上更改值。

如果要播放不同的音乐,需要加载:

if(music.getStatus() == sf::SoundSource::Status::Stopped)
{
    music.openFromFile("Music/CORE.ogg");
    music.play();
}