why this code not playing voice of cat i.e. when selected CAT from ListBox and p

Started by Nabeel Nazir, 09 September 2013, 08:22:32

Nabeel Nazir

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






texus

You are recreating the SoundBuffer every frame.

The sound is temporary as well.
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).
sf::SoundBuffer buffer;
sf::Sound sound;

Nabeel Nazir

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







texus

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.

Nabeel Nazir

 :(  :(

texus

Seems like the wav file is currupted. Try with another file and see if it works.

Nabeel Nazir

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


Nabeel Nazir

now i have downloaded another new wave file from this source https://soundbible.com/1287-Cat-Meow.html
now my program is giving this kind of error ! :(

texus

You'll have to wait till someone responds on the sfml forum, because I can't help with that.

Nabeel Nazir

hmmmmmmmmm !  :-[

Nabeel Nazir

 i didn't get the last reply of SFML developers team ! the link for the reply is as follows.
https://en.sfml-dev.org/forums/index.php?topic=12904.msg90497#msg90497

texus

I guess you understand it by now with the other reply on the sfml forum?

Nabeel Nazir

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 ! :(

#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

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.