SFML community forums

Help => Audio => Topic started by: spoty76 on March 26, 2017, 01:40:58 pm

Title: Music Play
Post by: spoty76 on March 26, 2017, 01:40:58 pm
Hello, I want to play music when my player dies, I have this in Int main
 
int main()
{
        Music death;
        death.openFromFile("music/death.ogg");
and I want to put death.play(); here
while (window.isOpen())
        {

                float time = clock.getElapsedTime().asMicroseconds();
                if (p.life == true) gameTime = gameTimeClock.getElapsedTime().asSeconds();
                else {
                        p.sprite.setColor(Color(235, 65, 7));
                       
                        view.zoom(0.999f);
                        view.move(0, 0.03);
                        view.move(0.03, 0);
                }

but
if (p.life == true) gameTime = gameTimeClock.getElapsedTime().asSeconds();
                else {
death.play();
wouldn't work... Can you please tell me what to do in details, because I'm just starting to learn programming.

Some details: Visual studio 2013, win 10, and I have class Player in my code.
Title: Re: Music Play
Post by: Hapax on March 26, 2017, 02:28:35 pm
wouldn't work
What does this mean? What does it do instead of what you expect it to do? Crash? Hear nothing? Error message?

Are you certain that the music file is opening correctly?
Remember to test the return value of openFromFile():
if (!death.openFromFile("music/death.ogg"))
{
    std::cerr << "\"music/death.ogg\" could not be opened!" << std::endl;
    return EXIT_FAILURE;
}
Title: Re: Music Play
Post by: spoty76 on March 26, 2017, 10:40:49 pm
I expect the music to play when p.life==true, it doesn't play, i hear nothing, yes I checked file destination, and it opens correctly, and I checked the file - it's not broken
Title: Re: Music Play
Post by: dabbertorres on March 27, 2017, 03:40:58 am
Well you aren't ever calling death.play() inside of the if statement checking p.life == true, so it won't ever play.
Title: Re: Music Play
Post by: Hapax on March 27, 2017, 12:27:54 pm
I expect the music to play when p.life==true, it doesn't play, i hear nothing
If you expect it to play when p.life is true, it should be played from the block following the if, not in the code block following the else (plays when it is not true).

Note also that if the music has begun, telling it to play again will restart it from the beginning. If this happens every cycle, it's likely the music won't get far enough into the sound for anything to be heard.

yes I checked file destination, and it opens correctly, and I checked the file - it's not broken
I don't mean that you checked it manually or once but instead that you should check it every time it is opened. I even gave you the code that you can use :P