Loading sound with Mix_LoadWav

I have recently started proramming with c++ and SDL and i am trying to learn by making a simple RPG game. In my sound-class i want to be able to play backgroundmusic and soundFx when needed. I am able to get the backgroundmusic running perfectly with Mix_Music and Mix_LoadMus(), but when attempting to load sound with Mix_Chunk and Mix_LoadWav() the program crashes instantly on startup.

i know the soundfiles works fine cause they work in the Mix_Music functions. Please help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//cpp file
  #include "sound.h"
#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"


sound::sound()
{
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 4096);
}

void sound::loadSound()
{
   backgroundMusic = Mix_LoadMUS("Data/Sound/tyrolenBackground.mp3");
    walkSound = Mix_LoadWAV("Data/Sound/walk.wav");
}

//Header file
#ifndef SOUND_H
#define SOUND_H

#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"

class sound
{
    public:
        sound();
        void loadSound();
        void setVolume();
        void playMusic();
        void playSound(bool walk);
        virtual ~sound();

    private:
        Mix_Music *backgroundMusic;
        Mix_Chunk *walkSound;
};
Make sure the file is loaded correctly.
1
2
3
4
5
6
7
8
9
walkSound = Mix_LoadWAV("Data/Sound/walk.wav");
if(walkSound)
{
	std::cout << "walk.wav was loaded successfully! " << std::endl;
}
else
{
	std::cout << "walk.wav error: " << Mix_GetError() << std::endl;
}

Last edited on
Not sure why, but the messages don't show up.. seems like it is because the the program crashes before the program comes to the std::cout..
If you are on Windows all output is redirected to a file named stdout.txt.
Yes, i noticed.. I also noticed that when i misspelled the file i wanted to load, i got the error message, but when everything is spelled correctly and should run fine, both the stdout.txt and stderr.txt are completely blank.
The easiest way to find out where it's crashing is to use a debugger. The alternative is to place print statements in your code until you have narrowed it down.
I tried to use a debugger, and everythink seems to work fine, except some minor faults in other classes. The debugger doesnt say anything about the soundclass..
Last edited on
So it doesn't crash when you run it with a debugger attached? What do you mean by "minor faults"? Are you sure they are harmless?
Topic archived. No new replies allowed.