1
Audio / Setting LoopPoints more than once will cause the music to stop playing.
« on: May 08, 2020, 06:15:27 pm »
I'm currently working on a small game and what I want is for the music to loop in small segments, with each segment building up the music.
The solution was to set loop points when playing a track and then after a specific event change the loop points to further along in the song.
The solution worked, anytime new loop points were set the music would keep on playing up until the end of the new loop point where it would jump back to start of the new loop point. However, the music would stop playing eventually, even though no functions were called to stop the sound.
I assumed it was just something in my engine, but I've modified the sound example to only play my music file and update the loop points every 25 seconds, and the music eventually stops (even the music status no longer returning the Playing state). I've attached the sample code and file in the post.
I'm using the latest SFML source code (last update 15th April 2020) and running on Windows 10, using Visual Studio 2017 for dev.
I've found that setting the loop points only once does not cause any issues, but setting loop points to the same music stream more than once causes it to eventually stop.
The solution was to set loop points when playing a track and then after a specific event change the loop points to further along in the song.
The solution worked, anytime new loop points were set the music would keep on playing up until the end of the new loop point where it would jump back to start of the new loop point. However, the music would stop playing eventually, even though no functions were called to stop the sound.
I assumed it was just something in my engine, but I've modified the sound example to only play my music file and update the loop points every 25 seconds, and the music eventually stops (even the music status no longer returning the Playing state). I've attached the sample code and file in the post.
// Load an ogg music file
sf::Music music;
if (!music.openFromFile("resources/" + filename))
return;
music.setLoop(true);
music.setLoopPoints(sf::Music::TimeSpan(sf::seconds(0.0f), sf::seconds(8.0f)));
// Play it
music.play();
int level = 1;
// Loop while the music is playing
while (music.getStatus() == sf::Music::Playing)
{
// Leave some CPU time for other processes
sf::sleep(sf::milliseconds(25000));
level++;
float loopEnd = (level + 2) * 8.0f;
if (loopEnd <= music.getDuration().asSeconds())
{
music.setLoop(true);
music.setLoopPoints(sf::Music::TimeSpan(sf::seconds(loopEnd - 8.0f), sf::seconds(8.0f)));
}
}
sf::Music music;
if (!music.openFromFile("resources/" + filename))
return;
music.setLoop(true);
music.setLoopPoints(sf::Music::TimeSpan(sf::seconds(0.0f), sf::seconds(8.0f)));
// Play it
music.play();
int level = 1;
// Loop while the music is playing
while (music.getStatus() == sf::Music::Playing)
{
// Leave some CPU time for other processes
sf::sleep(sf::milliseconds(25000));
level++;
float loopEnd = (level + 2) * 8.0f;
if (loopEnd <= music.getDuration().asSeconds())
{
music.setLoop(true);
music.setLoopPoints(sf::Music::TimeSpan(sf::seconds(loopEnd - 8.0f), sf::seconds(8.0f)));
}
}
I'm using the latest SFML source code (last update 15th April 2020) and running on Windows 10, using Visual Studio 2017 for dev.
I've found that setting the loop points only once does not cause any issues, but setting loop points to the same music stream more than once causes it to eventually stop.