SFML Sound not playing (Operation not permitted)

  audio, c++, sfml

So I was trying out the SFML tutorial for playing a sound from a local file and came across a weird bug. Whenever I call the playSound function no sound is played and I get an error that I can’t find the solution to:

[ALSOFT] (EE) Failed to set real-time priority for thread: Operation not permitted (1)

The strange thing is that if I do the same thing in the main function the sound is played. What is going on here? Here are the only files used in the project, the .wav is just a simple sound file:

main.cpp

#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

void playSound() {
    sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("1-retro-arcade-casino-notification.wav"))
        return;
    sf::Sound sound;
    sound.setBuffer(buffer);
    sound.play();
}

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Notification test");

    sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("1-retro-arcade-casino-notification.wav"))
        return -1;
    sf::Sound sound;
    sound.setBuffer(buffer);
    sound.play();

    while (window.isOpen()) {
        sf::Event event;

        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            if (event.type == sf::Event::TextEntered) {
                char key = static_cast<char>(event.text.unicode);
                
                if (key == 'p') {
                    playSound();
                }

                if (key == 'q') {
                    window.close();
                }
            }
        }
    }

    return 0;
}

Makefile

all:
    g++ -lsfml-audio -lsfml-graphics -lsfml-window -lsfml-system -o main main.cpp

Source: Windows Questions C++

LEAVE A COMMENT