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

Author Topic: sf::sound won't play .ogg file  (Read 10044 times)

0 Members and 1 Guest are viewing this topic.

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
sf::sound won't play .ogg file
« 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();
        }
}      
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: sf::sound won't play .ogg file
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::sound won't play .ogg file
« Reply #2 on: April 17, 2015, 12:03:55 pm »
Play the sound once, not repeatedly.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: sf::sound won't play .ogg file
« Reply #3 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?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::sound won't play .ogg file
« Reply #4 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()
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.
« Last Edit: April 17, 2015, 12:53:27 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::sound won't play .ogg file
« Reply #5 on: April 17, 2015, 12:50:47 pm »
Move the call to play() outside the loop.

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: sf::sound won't play .ogg file
« Reply #6 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.
« Last Edit: April 17, 2015, 01:38:31 pm by OrientatedCookie »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11033
    • View Profile
    • development blog
    • Email
Re: sf::sound won't play .ogg file
« Reply #7 on: April 17, 2015, 02:53:40 pm »
Does VLC or similar media players, play back audio?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: sf::sound won't play .ogg file
« Reply #8 on: April 17, 2015, 03:23:38 pm »
yes  ;) They play fine

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: sf::sound won't play .ogg file
« Reply #9 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.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: sf::sound won't play .ogg file
« Reply #10 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();
}
 

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: sf::sound won't play .ogg file
« Reply #11 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)
« Last Edit: April 17, 2015, 04:40:51 pm by zsbzsb »
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

OrientatedCookie

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: sf::sound won't play .ogg file
« Reply #12 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.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: sf::sound won't play .ogg file
« Reply #13 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.
SFML / OS X developer

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: sf::sound won't play .ogg file
« Reply #14 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
« Last Edit: April 17, 2015, 06:58:24 pm by Jesper Juhl »

 

anything