why this code not playing voice of cat i.e. when selected CAT from ListBox and press speak button ?
Code: [Select]

#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;
}






*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
You are recreating the SoundBuffer every frame.

The sound is temporary as well.
Code: [Select]
else {
    sf::Sound sound;        // <-  Sound object gets created
    sound.setBuffer(buffer);
    sound.play();
}   // <- Sound object gets destroyed as it goes out of scope

You should declare the SoundBuffer and Sound objects at the beginning of the main function.
So these lines should be moved to earlier in your program (anywhere above the main loop).
Code: [Select]
sf::SoundBuffer buffer;
sf::Sound sound;

Re: why this code not playing voice of cat!
« Reply #2 on: 11 September 2013, 17:23:13 »
 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

Code: [Select]
#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: 11 September 2013, 17:52:06 by Nabeel Nazir »

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Does it print something in the command prompt? Its probably going to say that cat.wav can't be found.

If I remember correctly, then for Visual Studio you have to place the file in either "C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml\Debug" or "C:\Users\corleone\Documents\Visual Studio 2010\Projects\MySfml\MySfml\Release" depending on whether you are building in debug or release mode.

Re:
« Reply #4 on: 11 September 2013, 17:53:34 »
 :(  :(

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Seems like the wav file is currupted. Try with another file and see if it works.

Re:
« Reply #6 on: 11 September 2013, 18:01:38 »
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
 

Re:
« Reply #7 on: 11 September 2013, 18:10:08 »
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 ! :(

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
You'll have to wait till someone responds on the sfml forum, because I can't help with that.

Re:
« Reply #9 on: 11 September 2013, 18:50:34 »
hmmmmmmmmm !  :-[

Re:
« Reply #10 on: 12 September 2013, 08:27:12 »
 i didn't get the last reply of SFML developers team ! the link for the reply is as follows.
http://en.sfml-dev.org/forums/index.php?topic=12904.msg90497#msg90497

*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
I guess you understand it by now with the other reply on the sfml forum?

yes i got that .... 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 ! :(
Code: [Select]

#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;
}







*

texus

  • *****
  • 1143
    • View Profile
    • Texus's Blog
Please post on either the sfml forum or on this forum but not on both.
There is no need to ask the same question twice, by the time one replies you might already have a reply on the other forum.

I read both forums anyway.