SFML community forums

Help => Audio => Topic started by: Brukmoon on January 11, 2013, 09:21:33 pm

Title: [SFML] Audio file won't load
Post by: Brukmoon 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
Title: AW: [SFML] Audio file won't load
Post by: eXpl0it3r 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?
Title: Re: [SFML] Audio file won't load
Post by: Brukmoon 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.
Title: Re: [SFML] Audio file won't load
Post by: Laurent 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).
Title: Re: [SFML] Audio file won't load
Post by: Brukmoon 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.
Title: Re: [SFML] Audio file won't load
Post by: eXpl0it3r 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. ;)
Title: Re: [SFML] Audio file won't load
Post by: Brukmoon 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.