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


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.

I couldn't believe when they started doing this, I believe it was after 98/ME... and, yes, I used ME between 98 and XP, once upon a time.

[ deftware.org ]

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.

Oh boy. Sorry you had to explain that to me but you really didn't need to. Forgetting .wav was just a really dumb error on my part.

Anyway after adding .wav I still get the same error.

Why can I load images just fine and not sound files?

Advertisement

Also my main does have (int argc, char* args[]) parameters and args[0] shows my projects file path.

At the present moment, are you loading a single sound, or many sounds? It'd help if you would post the actual code.
Is it every sound that's failing to load? The 3rd sound? The only sound?

Since it is actually SDL_RWFromFile() that is failing, and Mix_LoadWAV() is actually overwriting the real error message, try this function instead and post what the error says:
Mix_Chunk *My_LoadWAV(const char *filepath)
{
	SDL_RWops *fileData = SDL_RWFromFile(filepath, "rb");
	if(!fileData)
	{
		std::cout << "My_LoadWAV() failed to call SDL_RWFromFile().\n\t\"" << SDL_GetError() << "\"" << std::endl;
		return NULL;
	}
	
	Mix_Chunk *chunk = Mix_LoadWAV_RW(fileData, 1);
	if(!chunk)
	{
		std::cout << "My_LoadWAV() failed to call Mix_LoadWAV_RW().\n\t\"" << Mix_GetError() << "\"" << std::endl;
		return NULL;
	}
	
	return chunk;
}

Ok so I included your code and I got the error "MyLoadWav() failed to call SDL_RWFromFile(). Couldn't open PlayerShot.wav".

At the moment I'm only loading one sound effect. I've tried with other sound files and I also get the same error.

This is the code the function where I try to load my sound file.


void Bullet::LoadBulletMedia(SDL_Renderer* ren)
{
//TheSoundManager::Instance()->LoadSound("PlayerShot.wav", "0", SOUND_MUSIC); 
MyLoadWav("PlayerShot.wav"); 
laser = resource->LoadImage("laser.bmp", ren, 255, 0, 0);
}

This is my original function to load a sound file which I posted before:


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;
}

Really all I'm doing with that class is calling the Load function since I can't do anything else with it if the file isn't loaded.

It's saying that, for some reason, SDL's file opening function can't find or can't open your file.

Some of the possible reasons why it can't be opened:
- The file is locked by the operating system.
- The file doesn't exist where you are telling the code it exists. <<Most likely>>
- Your current directory is for some reason different than the directory of your executable.

Are you absolutely 100% sure in the exact same folder with your Game.exe file, you have a file named exactly "PlayerShot.wav" (without the quotes), and you have file extensions turned on? You don't want to accidentally rename your file "PlayerShot.wav.wav" because the real extension is accidentally invisible.

Oh, also, are you running your game from within your IDE? Some IDEs seem to run the executable with a different "current directory".

Try running the executable directly from the folder itself, to see if it makes a difference.

Is your "laser.bmp" file loading and displaying fine? Is your PlayerShot.wav file in the same folder as your laser.bmp file?

Advertisement

Now something new is happening. When I try to load a .mp3 file, the file actually gets loaded (however it doesn't play when I call PlayMusic). When I try to load a .wav file I get a new error which says "unknown Wave format".

My file extensions are turned on the file is called PlayerShot.wav and it is definitely in the same folder as all my images. My images also all load and get displayed.

This topic is closed to new replies.

Advertisement