I've found a solution. It's not clean and perfect, but ok. It works best with the click.wav which is attached to this post.
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
sf::RenderWindow mMainWindow(sf::VideoMode(200,200,32), "soundlooptest", sf::Style::Titlebar);
sf::Event mMainEvent;
sf::SoundBuffer soundBuffer;
if (!soundBuffer.loadFromFile("click.wav"))
return -1;
sf::Sound sound;
sound.setBuffer(soundBuffer);
const int button1_WIDTH = 100;
const int button1_HEIGHT = 100;
sf::Color button1_COLOR = sf::Color::Red;
sf::RectangleShape button1;
button1.setSize(sf::Vector2f(button1_WIDTH, button1_HEIGHT));
button1.setFillColor(button1_COLOR);
button1.setPosition(sf::Vector2f(0, 0));
sf::Clock clock;
sf::Time time;
time = clock.getElapsedTime();
while(mMainWindow.isOpen())
{
while(mMainWindow.pollEvent(mMainEvent))
{
if (button1.getGlobalBounds().contains(mMainWindow.mapPixelToCoords(sf::Mouse::getPosition(mMainWindow))))
{
if (clock.getElapsedTime().asMilliseconds() > 300.0f)
{
sound.stop();
sound.setLoop(false);
}
else
{
sound.play();
}
}
else
{
sound.setLoop(false);
clock.restart();
}
}
mMainWindow.clear();
mMainWindow.setFramerateLimit(30);
mMainWindow.draw(button1);
mMainWindow.display();
}
return 0;
}