Search code examples
c++classaudiosfml

The music doesn't start


When i run the programm the music doesn't star. I checked if the file is preloaded whitout problem . My file is .ogg whih is supported by sfml. I don't get why i have no sound when i run the program. There is no problem when i load the file

#pragma once

#include <SFML/Audio.hpp>

class Audio
{
public:
        Audio();
        ~Audio();
        bool    load();
        bool    gest();
private:
        sf::Music       _main;
};

bool    Audio::load()
{
    if (!_main.openFromFile("res/zelda.ogg"))
                return false;
        return true;
}

bool    Audio::gest()
{
        if (!load())
                return false;
        _main.play();
        return true;
}

int     main(void)
{
        Monitor         window;
        Audio           music;

        window.initWindow();
        while (window.run())
                music.gest();
}

Solution

  • You are calling music.gest() in a loop, which in turn calls load() every time around! You do call sf::SoundStream::play() in the loop as well, but the stream gets re-initialized every time.

    You shouldn't call play() in a loop anyway. SFML makes sure the sf::Music instance keeps streaming on its own thread.