C++ SDL Can play sound ??

I want to play sound with SDL_mixer, but I don't know why it doesn't play it..

here's the code:
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
#include <iostream>
#include <vector>

#include "define.h"

class CSoundManager {
    private:
        int channel;
        int audio_rate;
        Uint16 audio_format;
        int audio_channels;
        int audio_buffers;

        std::vector<sound> Sound;

    public:
        CSoundManager();

        void  soundBegin();
        void  soundEnd();

        void  loadSound(std::string);
        sound getSound(int);
        void  playSound(int);
        void  removeSound(int);
        void  removeAllSounds();
    };


1
2
3
4
5
6
7
8
9
#include "CSoundManager.h"

CSoundManager::CSoundManager()
{
    audio_rate = 22050;
    audio_format = AUDIO_S16SYS;
    audio_channels = 2;
    audio_buffers = 4096;
}


1
2
3
4
5
6
7
8
9
#include "CSoundManager.h"

void CSoundManager::loadSound(std::string filename)
{
    sound newSound;
    newSound = Mix_LoadWAV( filename.c_str() );

    Sound.push_back(newSound);
}


1
2
3
4
5
6
7
#include "CSoundManager.h"

void CSoundManager::playSound(int _soundId)
{

    Mix_PlayChannel(-1, Sound[_soundId], 0);
}


1
2
3
4
5
6
#include "CSoundManager.h"

void CSoundManager::soundBegin()
{
    Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers);
}


1
2
3
4
5
6
7
#include "CSoundManager.h"

void CSoundManager::soundEnd()
{
    Mix_CloseAudio();
}


In my initialize function I wrote this:
1
2
3
4
5
// Init
SoundManager.soundBegin();

// load sound
SoundManager.loadSound( "sound/sound1.wav" );


In function onEvent():
1
2
3
4
5
6
7
case SDL_KEYDOWN:
    switch( ev.key.keysym.sym )
    {
        case SDLK_w:
            SoundManager.playSound(0);
            break;
    }


When I run, and press 'w' key I here just crackle for half a second and then it stops.

Note: In main I declared CSoundManager SoundManager;
"sound" is: typedef Mix_Chunk * sound;
Last edited on
1. Do you know the specific piece of code where the problem is?
2. If it isn't too big of a program could you send it? strongdrinkdev@gmail.com
3. Try SFML, I like it a lot more than SDL and its more modern :D
I Solved a problem... but don't know how... it is working now :D
Congrats! (mark as solved please :P)
Topic archived. No new replies allowed.