SFML community forums

Help => Audio => Topic started by: MrOnlineCoder on December 22, 2017, 01:34:49 pm

Title: sf::Music hangs the app with PhysFS
Post by: MrOnlineCoder on December 22, 2017, 01:34:49 pm
Hello! I am working on a game and I load all resources from zip file using PhysFS. I am using this class from SFML wiki: https://github.com/SFML/SFML/wiki/Source:-PhysicsFS-Input-Stream (https://github.com/SFML/SFML/wiki/Source:-PhysicsFS-Input-Stream)

And I have noticed strange behaivour: if I open PhysFsStream once, open music from it, then everything is OK. But if I do this again (to change current music) -  my application hangs (100% of CPU core).

Minimal code (all variables are class members):

//GameState

//init() - called once when game state is shown up
pfsStream.open("music/1.ogg");
music.openFromStream(pfsStream);

music.play();

//input() - called on input.
music.stop();
pfsStream.open("music/2.ogg");
music.openFromStream(pfsStream);

music.play();
 

My PhysFS setup is fully wokrable (I use the same way to load textures, sounds, they are all working and loaded correctly). 

After debugging application for some time, I ended on this code in sf::SoundStream:

void SoundStream::stop()
{
    // Request the thread to terminate
    {
        Lock lock(m_threadMutex);
        m_isStreaming = false;
    }
...
 

I wil really appreciate any help concerning this topic, maybe newer version of PhysFS stream or any possible fixes.
Title: Re: sf::Music hangs the app with PhysFS
Post by: Ceylo on January 28, 2018, 11:10:36 pm
Did you try to pause your app while it's using 100% CPU to see what's the callstack of the different threads at that moment?
Title: Re: sf::Music hangs the app with PhysFS
Post by: FRex on January 29, 2018, 01:09:26 am
I just did this and it works for me as expected (it plays first song, I press enter once and then the second song plays until I press enter again and the program quits). You need to investigate deeper, test this code and give us a self contained example that fails.

int main(int argc, char ** argv)
{
    if(!PHYSFS_init(argv[0]))
    {
        std::fprintf(stderr, "%s\n", PHYSFS_getLastError());
        return 1;
    }

    if(!PHYSFS_addToSearchPath("test.zip", true))
    {
        std::fprintf(stderr, "%s\n", PHYSFS_getLastError());
        return 1;
    }

    PhysFsStream stream;
    sf::Music music;
    std::string a;

    stream.open("music/test1.ogg");
    music.openFromStream(stream);
    music.play();

    std::cout << "press enter to continue" << std::endl;
    std::getline(std::cin, a);

    music.stop();
    stream.open("music/test2.ogg");
    music.openFromStream(stream);
    music.play();

    std::cout << "press enter to continue" << std::endl;
    std::getline(std::cin, a);
}
 
Title: Re: sf::Music hangs the app with PhysFS
Post by: jonc on August 30, 2021, 02:11:08 am
I am facing the exact issue MrOnlineCoder is having. It works fine on Windows but on Android and MacOS the app hangs when switching to open a second song.

Edit: I did some debugging and noticed the onSeek(Time::Zero) in SoundStream::stop() was being called and it eventually calls PhysFsStream::seek and loops there forever for some reason. I removed the onSeek(Time::Zero) in SoundStream::stop() and now the app doesn't hang anymore on Android.