I am trying to make a small program that plays a sound whenever a key is pressed. I am mostly doing this to learn SFML and get better at cpp.
Looking at similar problems I have both the sf::SoundBuffer and sf::Sound as standalone members in a game class. The code works enough that the keystrokes are being registered (tested them by having them just close the window) but it refuses to play any sound. I have double checked the location of the sound files I am calling and it checks out. Any help would be greatly appreciated!
below is how I am loading the audio and the keypresses
void Game::loadSound(sf::SoundBuffer buff, sf::Sound sound, std::string file)
{
buff.loadFromFile(file);
sound.setBuffer(buff);
sound.setVolume(50);
}
void Game::initSounds()
{
loadSound(buffer1, drum1, "../sounds/Drum1.wav");
loadSound(buffer2, drum2, "../sounds/Drum2.wav");
loadSound(buffer3, drum3, "../sounds/Drum3.wav");
loadSound(buffer4, drum4, "../sounds/Drum4.wav");
loadSound(buffer5, spock, "../sounds/Spock.wav");
loadSound(buffer6, spike, "../sounds/Spike.wav");
}
void Game::pollEvents()
{
//Event Polling
while (this->window->pollEvent(this->event))
{
switch(this->event.type)
{
case sf::Event::Closed:
this->window->close();
break;
case sf::Event::KeyPressed:
switch(this->event.key.code)
{
case sf::Keyboard::Escape:
this->window->close();
break;
case sf::Keyboard::Num1:
drum1.play();
break;
case sf::Keyboard::Num2:
drum2.play();
break;
case sf::Keyboard::Num3:
drum3.play();
break;
case sf::Keyboard::Num4:
this->drum4.play();
break;
case sf::Keyboard::Num0:
this->spike.play();
break;
case sf::Keyboard::Num9:
this->spock.play();
break;
}
// if(event.key.code == sf::Keyboard::Escape)
// this->window->close();
break;
}
}
}