I am using Visual Studio to run a simple SFML code that renders a circle and plays audio. I am trying to play an audio before the rendering happens and after the rendering window is closed,
But for some reason after I close the rendering window the audio is not being played.
#include <iostream>
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
int main()
{
// playing audio
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("audio/powerUp.wav"))
{
std::cout << "Failed to load audio filen";
return -1;
}
else
{
std::cout << "Audio loadedn";
}
sf::Sound sound;
sound.setBuffer(buffer);
sound.setVolume(10.0f);
sound.play(); // works
// rendering a circle (Test)
sf::RenderWindow window(sf::VideoMode(600, 600), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
sound.play(); // not works
return 0;
}
I have linked lib and include files to the project and have also added below files in Linker
> Input
> Additional Dependencies
sfml-audio-d.lib;sfml-graphics-d.lib;sfml-window-d.lib;sfml-system-d.lib;sfml-network-d.lib;
Include and lib path added to Visual Studio Config
And using C++ 17 compiler
Any idea why this is happening?