BroColly BroColly - 1 year ago 94
C++ Question

Sounds not playing on my code

I was learning SFML library version 2.2, but the sounds won't playing. I compile, and run the program on ubuntu 14.04. Here's my program

#include "SFML/Audio.hpp"
#include <stdio.h>
#include <iostream>

using namespace std;
using namespace sf;

SoundBuffer buffer;
Sound audio;

int main(int argc, char* argv[]) {

cout << "Sounds Loaded" << endl;
cout << "Yay!" << endl;
return 0;


and here's my compiling code:

g++ testaudio.cpp -I <My Directory>/SFML-2.2/include/ -L <My Directory>/SFML-2.2/lib/ -lsfml-audio -lsfml-system -o testaudio

There's no error in my compiling but when I run the program, sounds not coming out. I checked my volume, but nothing has change.

Answer Source

The SFML sound tutorials say that sounds and music play in a separate thread. What's likely happening is your program runs, starts playing the sound on a separate thread, but the main thread keeps running and hits the end of the main function and exits, killing your program. In short: your program is exiting so quickly you don't even hear any sound playing.

Try calling sf::sleep() to give your program time to play the song before exiting.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download