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

Author Topic: why this code not playing voice of cat ?  (Read 6579 times)

0 Members and 1 Guest are viewing this topic.

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
why this code not playing voice of cat ?
« on: September 09, 2013, 08:20:28 am »
why this code not playing voice of cat i.e. when selected CAT from ListBox and press speak button ?

#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
//      {
//              //std::cout << "Hello world" << std::endl;
//      }
//
//             
//             
//}

int main()
{
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");

       

    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
        //button->setCallbackId(1);
        //button->bindCallbackEx(function, tgui::Button::LeftMouseClicked);

        tgui::ListBox::Ptr listBox(gui);
        listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
        listBox->addItem("LION");
        listBox->addItem("GOAT");
        listBox->setCallbackId(2);
        //listBox->getSelectedItem();
        //listBox->bindCallbackEx(function, tgui::ListBox::ItemSelected);
       
        tgui ::Callback callback;
       
        button->bindCallback(tgui::Button::LeftMouseClicked);
        button->setCallbackId(1);
        while (gui.pollCallback(callback))
        {

               
        /*      listBox->bindCallback(tgui::ListBox::ItemSelected);
                listBox->setCallbackId(2);*/

               
                sf::SoundBuffer buffer;
                if(callback.id==1)
                {
                        if (listBox->getSelectedItem()=="CAT")
                        {
                                if (!buffer.loadFromFile("cat.wav"))
                                {
                                        std::cout << "Error while loading music" << std::endl;
                                }
                                else
                                {
                                        sf::Sound sound;
                                        sound.setBuffer(buffer);
                                        sound.play();
                                }
                                       
                        }
                }
               
        }
       


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

            // Pass the event to all the widgets (if there would be widgets)
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
        }
        return EXIT_SUCCESS;
}





 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: why this code not playing voice of cat ?
« Reply #1 on: September 09, 2013, 08:25:57 am »
Because your sound and sound buffer are destroyed immediately after you start playing. You must keep them alive (i.e. declare them in a bigger scope) as long as they are in use.
Laurent Gomila - SFML developer

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: why this code not playing voice of cat ?
« Reply #2 on: September 09, 2013, 08:33:47 am »
Because your sound and sound buffer are destroyed immediately after you start playing. You must keep them alive (i.e. declare them in a bigger scope) as long as they are in use.
Yeah... they would be destroyed immediately... if they were even created at all. Look closer at the code, I think it is obvious to anybody who can program in any language and understands basic control structures/program flow why this doesn't work.

Until you learn how to program, don't venture near the sites of libraries that are meant as additions to the language itself. Do yourself and us the favour, please.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #3 on: September 11, 2013, 05:24:15 pm »
why this code not playing voice of cat i.e. when selected CAT from ListBox and press speak button (its not working now agaain)!
I PLACE MY CAT.WAV FILE AT THIS PATH 
C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml

#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
//      {
//              //std::cout << "Hello world" << std::endl;
//      }
//
//             
//             
//}

int main()
{
        sf::SoundBuffer buffer;
        sf::Sound sound;
        sound.setBuffer(buffer);

    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");

       

    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
        //button->setCallbackId(1);
        //button->bindCallbackEx(function, tgui::Button::LeftMouseClicked);

        tgui::ListBox::Ptr listBox(gui);
        listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
        listBox->addItem("LION");
        listBox->addItem("GOAT");
        listBox->setCallbackId(2);
        //listBox->getSelectedItem();
        //listBox->bindCallbackEx(function, tgui::ListBox::ItemSelected);
       
        tgui ::Callback callback;
       
        button->bindCallback(tgui::Button::LeftMouseClicked);
        button->setCallbackId(1);
       
       
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
                        if(event.key.code==sf::Keyboard::P)
                                sound.play();
            // Pass the event to all the widgets (if there would be widgets)
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
        }

        while (gui.pollCallback(callback))
        {

               
        /*      listBox->bindCallback(tgui::ListBox::ItemSelected);
                listBox->setCallbackId(2);*/

               
                if(callback.id==1)
                {
                        if (listBox->getSelectedItem()=="CAT")
                        {
                                if (!buffer.loadFromFile("cat.wav"))
                                {
                                        std::cout << "Error while loading music" << std::endl;
                                }
                                else
                                {
                                        sound.play();
                                }
                                       
                        }
                }
               
        }
       


        return EXIT_SUCCESS;
}






 
« Last Edit: September 11, 2013, 05:51:53 pm by Nabeel Nazir »

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #4 on: September 11, 2013, 06:01:13 pm »
i have placed cat.wav file now at both addresses!! Still its not working !
C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml\Debug
C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml\Release
 

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #5 on: September 11, 2013, 06:10:33 pm »
now i have downloaded another new wave file from this source http://soundbible.com/1287-Cat-Meow.html
now my program is giving this kind of error ! :(

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #6 on: September 11, 2013, 07:06:24 pm »
please help me kindly !

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: why this code not playing voice of cat ?
« Reply #7 on: September 11, 2013, 10:41:10 pm »
Please don't post four times within two hours, use the edit function instead. The people in this forum help voluntarily, so be more patient!
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Gobbles

  • Full Member
  • ***
  • Posts: 132
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #8 on: September 12, 2013, 02:00:13 am »
Have you tried adding breakpoints to see if it ever enters the callback code and loads the file?

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #9 on: September 12, 2013, 07:54:46 am »
yes i did this .... file is loading ! but there is an error ... i am attaching its print screen and giving its code !
#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
//      {
//              //std::cout << "Hello world" << std::endl;
//      }
//
//             
//             
//}

int main()
{
        sf::SoundBuffer buffer;
        sf::Sound sound;
        sound.setBuffer(buffer);
        sound.setPitch(1.5f);
        sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");

       

    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
        //button->setCallbackId(1);
        //button->bindCallbackEx(function, tgui::Button::LeftMouseClicked);

        tgui::ListBox::Ptr listBox(gui);
        listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
        listBox->addItem("LION");
        listBox->addItem("GOAT");
        listBox->setCallbackId(2);
        //listBox->getSelectedItem();
        //listBox->bindCallbackEx(function, tgui::ListBox::ItemSelected);
       
        tgui ::Callback callback;
       
        button->bindCallback(tgui::Button::LeftMouseClicked);
        button->setCallbackId(1);
       
       
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
                        if(event.key.code==sf::Keyboard::P)
                                sound.play();
            // Pass the event to all the widgets (if there would be widgets)
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
        }

        while (gui.pollCallback(callback))
        {

               
        /*      listBox->bindCallback(tgui::ListBox::ItemSelected);
                listBox->setCallbackId(2);*/

               
                //std::cout<<"heloooo!";
                if(callback.id==1)
                {
                        if (listBox->getSelectedItem()=="CAT")
                        {
                                //std::cout<<"heloooo!";
                                if (!buffer.loadFromFile("Cat Meow.wav"))
                                {
                                        std::cout << "Error while loading file" << std::endl;
                                }
                                else
                                {
                                        sound.play();
                                }
                                       
                        }
                        else
                        {
                                std::cout<<"aceesing CAT failed!"<<std::endl;

                        }
                }
                else
                {
                        std::cout<<"id is not valid!!!!"<<std::endl;


                }
               
        }
       

        //getch();
        return EXIT_SUCCESS;
}


 :(



 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: why this code not playing voice of cat ?
« Reply #10 on: September 12, 2013, 08:06:06 am »
You should attach the sound buffer to the sound after loading it.

Admitedly, this behaviour is not very well documented.
Laurent Gomila - SFML developer

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #11 on: September 12, 2013, 08:23:00 am »
sorry i didn't get you???
i am a beginner in SFML that is why having these problems !
« Last Edit: September 12, 2013, 08:24:57 am by Nabeel Nazir »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: why this code not playing voice of cat ?
« Reply #12 on: September 12, 2013, 08:27:10 am »
This

sf::SoundBuffer buffer;
sf::Sound sound;
...
buffer.loadFromFile(...);
sound.setBuffer(buffer);

Instead of

sf::SoundBuffer buffer;
sf::Sound sound;
sound.setBuffer(buffer);
...
buffer.loadFromFile(...);
Laurent Gomila - SFML developer

Rayan jutt

  • Newbie
  • *
  • Posts: 15
    • View Profile
    • Email
Re: why this code not playing voice of cat ?
« Reply #13 on: September 13, 2013, 04:50:39 pm »
NOW THERE IS NO ERROR ON CONSOLE ... BUT STILL IT IS NOT PLAYING THE VOICE OF CAT WHEN CAT ITEM IS SELECTED FROM LISTBOX AND SPEAK BUTTON HAS BEEN PRESSED ! DONT KNOW WHAT DO TO ! :(
 
#include <SFML/Audio.hpp>
#include <TGUI/TGUI.hpp>
#include <conio.h>

//void function(const tgui::Callback& callback)
//{
//    if (callback.id == 1)
//      {
//              //std::cout << "Hello world" << std::endl;
//      }
//
//             
//             
//}


int main()
{

        sf::SoundBuffer buffer;
        sf::Sound sound;
        //sound.setBuffer(buffer);
        sound.setPitch(1.5f);
        sound.setVolume(80);
    // Create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
    tgui::Gui gui(window);

    // Load the font (you should check the return value to make sure that it is loaded)
    gui.setGlobalFont("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/fonts/DejaVuSans.ttf");

       

    tgui::Button::Ptr button(gui);
    button->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        button->setPosition(250, 350);
    button->setText("SPEAK");
    button->setSize(300, 40);
        //button->setCallbackId(1);
        //button->bindCallbackEx(function, tgui::Button::LeftMouseClicked);

        tgui::ListBox::Ptr listBox(gui);
        listBox->load("C:/Users/corleone/Documents/Visual Studio 2010/Projects/MySfml/MySfml/TGUI-0.6-alpha2-Visual-C++10-2010-32-bits/TGUI-0.6-alpha2/widgets/Black.conf");
        listBox->setSize(150, 120);
    listBox->setItemHeight(30);
    listBox->setPosition(300, 100);
    listBox->addItem("HORSE");
    listBox->addItem("CAT");
    listBox->addItem("DONKEY");
        listBox->addItem("LION");
        listBox->addItem("GOAT");
        listBox->setCallbackId(2);
        //listBox->getSelectedItem();
        //listBox->bindCallbackEx(function, tgui::ListBox::ItemSelected);
       
        tgui ::Callback callback;
       
        button->bindCallback(tgui::Button::LeftMouseClicked);
        button->setCallbackId(1);
       
       
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
                        /*if(event.key.code==sf::Keyboard::P)
                                sound.play();*/

            // Pass the event to all the widgets (if there would be widgets)
            gui.handleEvent(event);
        }

        window.clear();

        // Draw all created widgets
        gui.draw();

        window.display();
        }

        while (gui.pollCallback(callback))
        {

               
        /*      listBox->bindCallback(tgui::ListBox::ItemSelected);
                listBox->setCallbackId(2);*/

               
                //std::cout<<"heloooo!";
                if(callback.id==1)
                {
                        if (listBox->getSelectedItem()=="CAT")
                        {
                                //std::cout<<"heloooo!";
                                if (!buffer.loadFromFile("Cat Meow.wav"))
                                {
                                        sound.setBuffer(buffer);
                                        std::cout << "Error while loading file" << std::endl;
                                }
                                else
                                {
                                        sound.play();
                                }
                                       
                        }
                        else
                        {
                                std::cout<<"aceesing CAT failed!"<<std::endl;

                        }
                }
                else
                {
                        std::cout<<"id is not valid!!!!"<<std::endl;


                }
               
        }
       

        //getch();
        return EXIT_SUCCESS;
}






 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: why this code not playing voice of cat ?
« Reply #14 on: September 13, 2013, 04:55:21 pm »
Your "GUI loop" (while (gui.pollCallback(callback))) will only be executed after your window has been closed. So basically the corresponding code is never executed.

And please write with regular characters, there's no need to use upper case which only make us feel like you're screaming at us.
Laurent Gomila - SFML developer