SFML community forums

Help => Audio => Topic started by: OrientatedCookie on April 17, 2015, 11:47:15 am

Title: sf::sound won't play .ogg file
Post by: OrientatedCookie on April 17, 2015, 11:47:15 am
I am trying to play an .ogg file in SFML, I have all the DLLs and the libraries etc, but for some reason nothing plays when I start the code.  I have read the tutorials and have looked around a bit to see if I could fix it myself, but had no avail. I think it might have something to do with the buffer in the wrong place, but not sure. Furthermore the window freezes when opened, don't know what is wrong. Any ideas?

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/Audio.hpp>



int main()
{
        sf::Window window(sf::VideoMode(800, 600), "My window");

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

        while (window.isOpen())
        {


               
                sound.play();
        }
}      
 
Title: Re: sf::sound won't play .ogg file
Post by: eXpl0it3r on April 17, 2015, 11:50:23 am
Is you system volume set to something > 0? Are your speakers/headphones working? ;)

The window freezes because you're not processing events.
Title: Re: sf::sound won't play .ogg file
Post by: Nexus on April 17, 2015, 12:03:55 pm
Play the sound once, not repeatedly.
Title: Re: sf::sound won't play .ogg file
Post by: OrientatedCookie on April 17, 2015, 12:15:34 pm
Thank you for the responses, my volume isn't the problem I don't think. How would I play the sound just once?
Title: Re: sf::sound won't play .ogg file
Post by: Nexus on April 17, 2015, 12:50:28 pm
Call play() once, not in a loop...

And this is explained in the documentation, please read it next time:
Quote from: sf::Sound::play() (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. This function uses its own thread so that it doesn't block the rest of the program while the sound is played.
Title: Re: sf::sound won't play .ogg file
Post by: Jesper Juhl on April 17, 2015, 12:50:47 pm
Move the call to play() outside the loop.
Title: Re: sf::sound won't play .ogg file
Post by: OrientatedCookie on April 17, 2015, 01:32:57 pm
I moved the sound.play(); outside of the loop, but still not hearing anything  :-\ I also tried with sf::music, and with different files.
Title: Re: sf::sound won't play .ogg file
Post by: eXpl0it3r on April 17, 2015, 02:53:40 pm
Does VLC or similar media players, play back audio?
Title: Re: sf::sound won't play .ogg file
Post by: OrientatedCookie on April 17, 2015, 03:23:38 pm
yes  ;) They play fine
Title: Re: sf::sound won't play .ogg file
Post by: zsbzsb on April 17, 2015, 03:46:23 pm
I moved the sound.play(); outside of the loop, but still not hearing anything  :-\ I also tried with sf::music, and with different files.

Show your updated code. Alternatively try the included examples that come with SFML.
Title: Re: sf::sound won't play .ogg file
Post by: OrientatedCookie on April 17, 2015, 04:30:07 pm
I moved the sound.play(); outside of the loop, but still not hearing anything  :-\ I also tried with sf::music, and with different files.

Show your updated code. Alternatively try the included examples that come with SFML.

Okay thanks, here is my code
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

int main()
{
        // create the window
        sf::RenderWindow window(sf::VideoMode(800, 600), "My window");

        sf::SoundBuffer buffer;
        buffer.loadFromFile("mine.ogg");
        sf::Sound sound;
        sound.setBuffer(buffer);
       
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        // check the type of the event...
                        switch (event.type)
                        {
                                // window closed
                        case sf::Event::Closed:
                                window.close();
                                break;
                        }
                }

        }



       
        sound.play();
}
 
Title: Re: sf::sound won't play .ogg file
Post by: zsbzsb on April 17, 2015, 04:38:20 pm
...............Now put a breakpoint on the call to sound.play() and tell me when it is called. Also you must learn about program flow control and basic C++ before you can even attempt to use SFML. So get yourself a good C++ book and learn that for 2 months and then come back to SFML.

(please no one else state the obvious)
Title: Re: sf::sound won't play .ogg file
Post by: OrientatedCookie on April 17, 2015, 04:50:09 pm
...............Now put a breakpoint on the call to sound.play() and tell me when it is called. Also you must learn about program flow control and basic C++ before you can even attempt to use SFML. So get yourself a good C++ book and learn that for 2 months and then come back to SFML.

(please no one else state the obvious)
The tutorial was more helpful than you. For your information, I know several languages of code and have been doing C++ for much longer than 2 months. Case closed.
Title: Re: sf::sound won't play .ogg file
Post by: Hiura on April 17, 2015, 05:33:55 pm
Listen to what zsbzsb said for your own good. Your mistake is quite big and it's safe to say that if you don't understand it now you'll run into much bigger problem later.

PS: I've been programming for ~10 years now and from time to time, as everybody, I learn something new. There's no shame in admitting you don't know something.
Title: Re: sf::sound won't play .ogg file
Post by: Jesper Juhl on April 17, 2015, 06:34:09 pm
So so tempting to point out the bleeding obvious... Arrgh, must resist..

Edit: ok, so yeah, that's kind of a dick comment. Here's something more constructive; maybe taking a look at my Jukebox class will help you: https://github.com/SFML/SFML/wiki/Source:-Jukebox