Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Music hangs the app with PhysFS  (Read 9563 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
sf::Music hangs the app with PhysFS
« 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

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.

Ceylo

  • Hero Member
  • *****
  • Posts: 2325
    • View Profile
    • http://sfemovie.yalir.org/
    • Email
Re: sf::Music hangs the app with PhysFS
« Reply #1 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?
Want to play movies in your SFML application? Check out sfeMovie!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: sf::Music hangs the app with PhysFS
« Reply #2 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);
}
 
Back to C++ gamedev with SFML in May 2023

jonc

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: sf::Music hangs the app with PhysFS
« Reply #3 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.
« Last Edit: August 30, 2021, 08:16:25 am by jonc »