SFML community forums

Help => Audio => Topic started by: paupav on April 22, 2014, 09:13:28 pm

Title: SFML not playing sound
Post by: paupav on April 22, 2014, 09:13:28 pm
So I have problem with SFML sound. 1 file
 plays normally and second one wont play while inside "while" function.

So this is before while function
 //sound
        wonb.loadFromFile("win.wav");
        wons.setBuffer(wonb);
        collectedb.loadFromFile("collected.wav");
        collecteds.setBuffer(collectedb);

and this is inside while function
collecteds.play();
wons.play();

For some reason collecteds play's normally and wons doesn't. Both are .wav formats downloaded from Soundbible.
This is the one (wonb/wons) doesn't work:
http://soundbible.com/1964-Small-Crowd-Applause.html

i've tryed few other with same result.

This is full code if anyone is interested:
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <cstdlib>

int main()
{
    pocetak:
    sf::ContextSettings settings;
    sf::Clock clock, clock2, clock3, clock4;


    settings.antialiasingLevel = 16;
    sf::RenderWindow window(sf::VideoMode(800, 600), "DogePav", sf::Style::Default, settings);

    //sound thingy
    sf::SoundBuffer wonb, collectedb;
    sf::Sound wons, collecteds;


    struct randomposition
    {
        int x;
        int y;
    }trir, octr, sqr;


    srand(time(0));

    trir.x = (rand() % 580)+ 40;
    trir.y = (rand() % 320) + 99;

    octr.x = (rand() % 260) + 40;
    octr.y = (rand() % 320) + 99;

    sqr.x = (rand() % 200) + 400;
    sqr.y = (rand() % 320) + 200;

    std::cout << " octr.x " << octr.x << " octr.y" << octr.y << std::endl;
    std::cout << " tri.x " << trir.x << " tri.y" << trir.y << std::endl;
    std::cout << " sqr.x " << sqr.x << " sqr.y" << sqr.y << std::endl;


    //objects
    sf::Vector2i tripos(trir.x, trir.y);
    sf::Vector2i octpos(octr.x, octr.y);
    sf::Vector2i sqpos(sqr.x, sqr.y);
    sf::Vector2i collected(760, 20);

    //bouncing
    bool returning = false;
    bool userControls = true;
    float positionx = 10;
    float positiony = 16;

        //defining objects
        sf::CircleShape triangle(15, 3);
        sf::CircleShape square(15, 4);
        sf::CircleShape octagon(15, 8);
        sf::CircleShape shape(30, 999);
        sf::Texture texture, texture2;

        if(!texture.loadFromFile("texture.png"))
            std::cout << "Could not load ball texture" << std::endl;

        shape.setTexture(&texture);
        //floor
        sf::RectangleShape floor(sf::Vector2f(800, 60));

        if(!texture2.loadFromFile("brick.png"))
            std::cout << "Could not load floor texture" << std::endl;

        floor.setTexture(&texture2);
        floor.setPosition(0, 540);

        octagon.setFillColor(sf::Color::Blue);
        triangle.setFillColor(sf::Color::Yellow);
        square.setFillColor(sf::Color::Magenta);

        //sound
        wonb.loadFromFile("win.wav");
        wons.setBuffer(wonb);
        collectedb.loadFromFile("collected.wav");
        collecteds.setBuffer(collectedb);


    while(window.isOpen())
    {


    wons.play();


        triangle.setPosition(tripos.x, tripos.y);
        octagon.setPosition(octpos.x,octpos.y);
        square.setPosition(sqpos.x,sqpos.y);

        //time
        sf::Time borderBounce = clock3.getElapsedTime();
        sf::Time time = clock.getElapsedTime();
        sf::Time physic = clock2.getElapsedTime();


        shape.setPosition(positionx, positiony);
        if(!returning)
        positiony = time.asMicroseconds() / 2319 + physic.asMilliseconds()/ 22;

        while(positionx < 2)
            positionx++;
        while(positionx > 650)
            positionx--;



       if(positiony > 480)
            returning = true;

        if(returning )
        {
            positiony -=8;
            clock.restart();
        }
        if(positiony < 15 + physic.asMilliseconds()/ 22)
        returning = false;


        if(physic.asSeconds() > 13)
        {
            goto pocetak;
        }

        sf::Event event;
        while(window.pollEvent(event))
        {

            switch(event.type)
            {
                case sf::Event::Closed:
                window.close();
            }
       //collect triangle


        //std::cout << "Triangle pos: " << positionx << " " << positiony <<std::endl;
        }


        if(tripos.x + 30 > positionx && positionx > tripos.x -30 && tripos.y + 30 > positiony && positiony > tripos.y - 30)
        {
            tripos.x = collected.x;
            tripos.y = collected.y;
            collecteds.play();
        }

        //collect oct
        if(octpos.x + 30 > positionx && positionx > octpos.x -30 && octpos.y + 30 > positiony && positiony > octpos.y - 30)
        {
            octpos.x = collected.x;
            octpos.y = collected.y * 6 - 20;
            collecteds.play();
        }
        //collect square
        if(sqpos.x + 30 > positionx && positionx > sqpos.x -30 && sqpos.y + 30 > positiony && positiony > sqpos.y - 30)
        {
            sqpos.x = collected.x;
            sqpos.y = collected.y * 3;
            collecteds.play();
        }



        //CONTROLLS
        if(userControls)
        {
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                positionx += 5;
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                positionx -= 5;
            std::cout.flush();

        }




    window.clear(sf::Color::White);
    window.draw(shape);
    window.draw(floor);
    window.draw(octagon);
    window.draw(triangle);
    window.draw(square);
    window.display();
    window.setFramerateLimit(60);

    }
}
 
Title: Re: SFML not playing sound
Post by: AlexxanderX on April 22, 2014, 09:17:30 pm
Maybe the mistake is that you play the wons sound every frame. What is the length of the sound?
Title: Re: SFML not playing sound
Post by: eXpl0it3r on April 22, 2014, 09:25:26 pm
Quote from: SFML Documentation (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Sound.php#a2953ffe632536e72e696fd880ced2532)
This function starts the stream if it was stopped, resumes it if it was paused, and restarts it from beginning if it was it already playing.

So yeah if you call play() in a loop the sound will constantly get restarted. If that happens 60 times per second, there's only a few milliseconds that could possibly be played, meaning in 99.99% of the cases you won't hear anything or just some noise.
Don't call play() if it's already playing.