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

Author Topic: Can't play 2 "musics" on one game?  (Read 3811 times)

0 Members and 1 Guest are viewing this topic.

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Can't play 2 "musics" on one game?
« on: April 06, 2020, 12:32:08 pm »
Hello,
While experimenting and making a little game, i wanted to start the game with a first music and further it at some point with a second one.

The musics work fine if the function music.play() is called before the while(window.isOpen()), but if i put a music.play() inside this "while" the music will not..play correctly. It will stop at every move the mouse cursor make. Meaning if the mouse hasn't moved the music plays, if the cursor moves, it will restart.

I decided then, to put a bool variable inside the  "while", and call the function music.play() after the while (after : while(window.isOpen() ) ). The condition would be for the bool variable to be true.

It does not work!

 bool part2=0;
    bool cond1=0;
    bool part3=0;
...
..

///MUSIC
    sf::Music Music2;
    if(Music2.openFromFile("data/music2.ogg")==0)
    {
        return 1;
    }

    Music2.setVolume(30);
    Music2.setLoop(true); ///
    Music2.play();
    //second music
    sf::Music Music3;
    if(Music3.openFromFile("data/music3.ogg")==0)
    {
        return 1;
    }
    Music3.setVolume(30);
    Music3.setLoop(true); ///
    Music3.play();
    Music3.pause(); /// i tried here to make the music start before the while then pause it, to see if could just resume the music later.

while (window.isOpen())
    {


        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();

            if (event.type== sf::Event::MouseButtonPressed && event.mouseButton.button ==sf::Mouse::Left)
          {
              ....
              ...
                  {if(position.y<=446&&position.y>=263){if(money>=300&&weight<=450){part2=0;cond1=1;money=money-300;}}}
               
                .....


        Music3.play(); /// would do the problem discribed above

        }

        ///LOGIC
        ....
        ...
          //  if(weight<=450&&cond1==0){part2=1;}

            if(cond1==1){part3=1; part2=0;}//Music2.stop();Music2.play();}  ... // it did not work here either
           // if(Music2.getStatus()==sf::Sound::Stopped)
            //{Music3.play();}  // does not play
             ...
..



 
« Last Edit: April 06, 2020, 12:34:13 pm by Power »

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Can't play 2 "musics" on one game?
« Reply #1 on: April 06, 2020, 05:08:40 pm »
If you tell a music to play when it's already playing, it will restart from the beginning.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Can't play 2 "musics" on one game?
« Reply #2 on: April 06, 2020, 11:09:57 pm »
If you tell a music to play when it's already playing, it will restart from the beginning.
Hello Mr Hapax,
Yes indeed, i actually noticed it when the function was inside the while.
But after that, the function was called Soo fast that it had not the time to play, i thought the game could not handle 2 musics..
Here is the solution in case anyone browse this forum in 2022 :
if(Music2.getStatus()==sf::Sound::Stopped&&Music3.getStatus()==sf::Sound::Stopped)
            {Music3.play();}
Meaning the music3 will not play only when the music2 has stopped, AND music3 will not play either if its not in a state of "stopped").
Thanks Hap
« Last Edit: April 06, 2020, 11:47:52 pm by Power »

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Can't play 2 "musics" on one game?
« Reply #3 on: April 07, 2020, 05:09:49 pm »
In case you want something a little more 'automatic', feel free to use:
https://github.com/Hapaxia/SfmlSoundSystem
It allows multiple musics and will also 'fade' from one to the next when you start a new one (you get to choose how long that takes!)
I also allows sounds to be played from there. Useful for sound effects.
It stores the resources inside it so this can be considered a sound resource manager that can also play them too!

Hello Mr Hapax,
...
Thanks Hap
I'm not a mister but you are welcome :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Power

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Re: Can't play 2 "musics" on one game?
« Reply #4 on: April 09, 2020, 02:22:40 pm »
Hello Mr Hapax,
...
Thanks Hap
I'm not a mister but you are welcome :)

You are amazing thanks for all the good work.



In case you want something a little more 'automatic', feel free to use:
https://github.com/Hapaxia/SfmlSoundSystem
It allows multiple musics and will also 'fade' from one to the next when you start a new one (you get to choose how long that takes!)
I also allows sounds to be played from there. Useful for sound effects.
It stores the resources inside it so this can be considered a sound resource manager that can also play them too!

So many things to learn, it's great! I don't use github alot...yet, i am supposed to download all the files, and then add the libraries (i am using codeblocks) the same way i have added the graphics and audio module, then not forget to to an "#include" on the code?

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Can't play 2 "musics" on one game?
« Reply #5 on: April 09, 2020, 10:11:21 pm »
i am supposed to download all the files, and then add the libraries (i am using codeblocks) the same way i have added the graphics and audio module, then not forget to to an "#include" on the code?
You can build download the files and build it as a library and then include them, yes. This is how I use it myself. It's more flexible as you can then use it easily in multiple projects.
It is a small thing though, so you could just copy it into a project and build it with it. Totally up to you :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*