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

Author Topic: [SFML] Audio file won't load  (Read 14656 times)

0 Members and 1 Guest are viewing this topic.

Brukmoon

  • Newbie
  • *
  • Posts: 6
    • View Profile
[SFML] Audio file won't load
« on: January 11, 2013, 09:21:33 pm »
Hello dear SFML programmers,

I am having an issue which I have been trying to resolve the past few days. I am not able to load/play music files.
Example:
Code: [Select]
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>

int main()
{
sf::RenderWindow window(sf::VideoMode(sf::VideoMode::getDesktopMode()), "SFML");
sf::SoundBuffer buffer;
    if (!buffer.loadFromFile("mainMusic.wav"))
        return -1;
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();
}
}
}

The program immediately closes therefore it is obvious that the sound file "mainMusic.wav" does not open.
Yes, the .wav file exists. I have all needed .dll files. I am running the SFML 2.0 version, Windows 7. I have included the .lib files for dynamic linkage.
Could you, please, help me?
Thank you in advance.

Sincerely yours,
Brukmoon

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: [SFML] Audio file won't load
« Reply #1 on: January 11, 2013, 10:05:12 pm »
What does the console output say?
With 'all needed DLLs' do you also mean openal and libsndfile?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Brukmoon

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [SFML] Audio file won't load
« Reply #2 on: January 12, 2013, 08:48:53 am »
Hello.

Yes, I have put all the DLLs into the folder with my .exe application.
As I've stated before the application compiles and links well, no errors/warning at all, build successful. It just won't open the file even thought it should.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML] Audio file won't load
« Reply #3 on: January 12, 2013, 09:15:39 am »
Check your working directory, especially if you run the project from the IDE (search in your project settings).
Laurent Gomila - SFML developer

Brukmoon

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [SFML] Audio file won't load
« Reply #4 on: January 12, 2013, 09:56:30 am »
Hello sir.

I have checked it.
Code: [Select]
C:\Users\Brukmoon\Documents\Visual Studio 2010\Projects\SFML\Release
In this directory I have everything, the DLLs, the 'mainMusic.wav' and the application. I will test the application on another computer.
I would like to thank you all for trying to help me. I really appreciate it.

PS: I have also tried the sf::Music. It did not work either.
UPDATE: I have tried to run the application on my second computer and it did not work there either.
« Last Edit: January 12, 2013, 10:09:12 am by Brukmoon »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: [SFML] Audio file won't load
« Reply #5 on: January 12, 2013, 10:10:23 am »
I have checked it.
C:\Users\Brukmoon\Documents\Visual Studio 2010\Projects\SFML\Release
Which is wrong, unless you've changed it manually in the project settings.
The default working directory with Visual Studio is next to the project file, in your case I'd say you'll have to put all the resources into the following directory:
C:\Users\Brukmoon\Documents\Visual Studio 2010\Projects\SFML

What does the console output say?
As I've stated before the application compiles and links well, no errors/warning at all, build successful. It just won't open the file even thought it should.
I wasn't talking about the compiler out here, I referred to the console output by the application. When the loading fails, then you'll always get a debug output on std::err, which will tell you what exactly went wrong. In your case it would probably state that the audio file couldn't have been found.

For example with the following code, your application doesn't just simply close when the loading fails, but it waits for 5 seconds, so you'd be able to read the error message.
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <SFML/System.hpp>
#include <iostream>

int main()
{
        sf::RenderWindow window(sf::VideoMode(sf::VideoMode::getDesktopMode()), "SFML");
        sf::SoundBuffer buffer;
        if (!buffer.loadFromFile("mainMusic.wav"))
        {
                std::cout << "Error while loading music" << std::endl;
                sf::sleep(sf::seconds(5));
                return -1;
        }
        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();
                }
        }
}

Btw. you should always post code with the tag code=cpp. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Brukmoon

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: [SFML] Audio file won't load
« Reply #6 on: January 12, 2013, 10:45:05 am »
Ohh, sorry, I've misunderstood.
Code: [Select]
The console shows the following error: "Failed to open sound file "mainMusic.wav" (file contains data in an unknown format).
Error while loading music.

That is pretty strange, the file has 'Wave' type (.wav). I will download another .wav file and test if it works.
Update: Oh my god, yes, the 'mainMusic.wav' was somehow corrupted. Thank you very much guys! The console really helped me.
« Last Edit: January 12, 2013, 10:49:28 am by Brukmoon »