Advertisement

Can't load .wav files with SDL

Started by November 06, 2014 09:01 PM
15 comments, last by Daniel Vellucci 10 years, 2 months ago

This is a problem that I only seem to have since I cannot find someone on google who has this problem. I can't load .wav files with SDL2_Mixer at all. I followed the lazyfoo tutorial. I even created a new project and re-downloaded the mixer files and the same happens.

Does anyone have possible solutions to this?

I can't load .wav files with SDL2_Mixer at all.

What happens when you try?

Is 'Mix_LoadWAV()' returning a null pointer? What's the output from Mix_GetError() telling you?

I followed the lazyfoo tutorial.

Could you post the exact code you are using in your project? Don't post the LazyFoo version, post the exact code you are trying to compile, copied from your project.
Advertisement
bool SoundManager::LoadSound(std::string fileName, std::string id, sound_type type)
{
if(type == SOUND_MUSIC) //if music is being loaded
{
Mix_Music* pMusic = Mix_LoadMUS(fileName.c_str()); //load the music

if(pMusic == 0) //if pointer is empty (Music can't be loaded)
{
std::cout << "Could not load music: ERROR - "
<< Mix_GetError() << std::endl;
return false;
}
else
{
m_music[id] = pMusic; //store the music into the map
return true;
}
}
else if(type == SOUND_SFX) //if sound effect is being loaded
{
Mix_Chunk* pChunk = Mix_LoadWAV(fileName.c_str());
if(pChunk == 0)
{
std::cout << "Could not load SFX: ERROR - "
<< Mix_GetError() << std::endl;
return false;
}
else
{
m_sfxs[id] = pChunk;
return true;
}
}
return false;
}


Thats what I use to load the sound files. Basically I'm just getting a null pointer. I changed the code from the lazyfoo version to this one ffrom a book but I still get the same thing. The exact error is Could not load SFX: Error - Mix_LoadWAV with NULL src.

EDIT: Sorry my code looks like that I wasn't sure how to post code properly.

You can wrap code in [ code ] and [ /code ] tags, or use the '<>' icon in the toolbar of the post editor. smile.png

So... Mix_LoadWAV() is returning a null pointer, which means it failed. Then, you're using Mix_GetError() to get the error message of why it failed.

The error message it gave you was "Mix_LoadWAV with NULL src", but I can't find much on Google about that.

How are you calling your SoundManager::LoadSound() function? Could you post the function call that is failing?

Actually, the error message should be "Mix_LoadWAV_RW with NULL src", shouldn't it? Did you accidentally copy the error message wrong? wacko.png

Are you positive your filepath is correct? Could you post the filepath you are using? It seems to be saying it that Mix_LoadWAV() isn't even receiving the raw binary data from the file. It's not a parsing issue, or anything like that - it's not even getting the file data.

Actually, the error message should be "Mix_LoadWAV_RW with NULL src", shouldn't it? Did you accidentally copy the error message wrong? wacko.png

Are you positive your filepath is correct? Could you post the filepath you are using? It seems to be saying it that Mix_LoadWAV() isn't even receiving the raw binary data from the file. It's not a parsing issue, or anything like that - it's not even getting the file data.

Try logprinting your fileName.c_str() to make sure it's correct.

In these cases sometimes I actually dive into SDL's actual code to see what causes certain errors.

[ deftware.org ]

Advertisement

Yeah that's what I did - I know that Mix_LoadWAV() isn't failing to parse the data. It's actually SDL_RWFromFile() failing - though because Mix_LoadWAV() overwrites the SDL_SetError() that SDL_RWFromFile() set, with Mix_LoadWAV()'s own generic error, why SDL_RWFromFile() is actually failing is unknown to me. Likely just an invalid filepath. It's usually the most obvious thing. biggrin.png

The line that calls the load function is TheSoundManager::Instance()->LoadSound("PlayerShot", "0", SOUND_SFX);

I put the the sound file into every folder of the project just incase I had put it into the wrong one but I get the same error.

The error is actually "Mix_LoadWAV_RW with NULL src".

How do you run the project?

If it is ran from Visual Studio with (CTRL+) F5, then the working directory is the directory that contains the project file.

In any case, SDL requires you to have int main(int, char** argv), so what is the value of argv[0] when you run your program?

Edit:

And the obvious question: do youhave a file called "PlayerShot" (no extension) in that directory?

The line that calls the load function is TheSoundManager::Instance()->LoadSound("PlayerShot", "0", SOUND_SFX);

You need to learn about file extensions.

A basic text file isn't just called "Bob", it's called "Bob.txt"

A wav file isn't just called "PlayerShot" it's called "PlayerShot.wav"

Every file on your computer potentially has a file extension.

The extension actually just part of the filename, and they don't have to correct - you can rename PlayerShot.wav to PlayerShot.EggMcMuffin and it'll still load fine. The extensions are more of a hint to programs about what kind of data the file potentially contains. However, since it's part of the filename, you need to make sure you get the entire filename (including the extension) correct, for some programs.

Your "PlayerShot" file in your folders is actually named "PlayerShot.wav". The Windows operating systems (Vista, Win7, Win8, etc...) by default hides the file extensions to prevent less computer-savvy users from accidentally changing the extension and confusing programs trying to open them.

Programmers need to be able to see file extensions, because of the work we do, so you'll want to tell Windows to, from now on, show you file extensions to you can see the full name of your files. Here's how to do that.

There are other options as well, that you actually don't want to enable. Don't enable "Show hidden files, folders, and drives". It won't benefit you at the present time, and might cause problems if you accidentally do something wrong.

The filepaths are relative to your program's executable

"PlayerShot.wav" //It'll look in the same folder where your executable is.

"/Sounds/PlayerShot.wav" //It'll look for a 'Sounds' folder where the executable is, and then look for 'PlayerShot.wav' in that 'Sounds' folder.

"../Sounds/PlayerShot.wav" //Starting at the executable, it'll go back one folder, then look for the 'Sounds' folder, and then look for the PlayerShot.wav file.

This topic is closed to new replies.

Advertisement