PLaying a sound file with SDL - C++

How do I play a sound file with SDL?
I've downloaded the library files but SDL/SDL_mixer.h (that most of the tutorials include) doesn't exist. So, how do I install it?

As I said mixer.h doesn't exist but, If it is helpful, the default code is this:

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

int main(int argc, char *argv[])
{
        SDL_Surface *screen;
        Mix_Chunk *sound = NULL;
        int channel;
        int audio_rate = 22050;
        Uint16 audio_format = AUDIO_S16SYS;
        int audio_channels = 2;
        int audio_buffers = 4096;
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
                printf("Unable to initialize SDL: %s\n", SDL_GetError());
                return 1;
        }
        if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {
                printf("Unable to initialize audio: %s\n", Mix_GetError());
                exit(1);
        }
        sound = Mix_LoadWAV("sound.wav");
        if(sound == NULL) {
                printf("Unable to load WAV file: %s\n", Mix_GetError());
        }
        screen = SDL_SetVideoMode(320, 240, 0, SDL_ANYFORMAT);
        if (screen == NULL) {
                printf("Unable to set video mode: %s\n", SDL_GetError());
                return 1;
        }
        channel = Mix_PlayChannel(-1, sound, 0);
        if(channel == -1) {
                printf("Unable to play WAV file: %s\n", Mix_GetError());
        }
        while(Mix_Playing(channel) != 0);
        Mix_FreeChunk(sound);
        Mix_CloseAudio();
        SDL_Quit();
        return 0;
}

Thanks for any replies....:slight_smile:

SDL_mixer is an additional thing to install. Try searching your package manager or see here.

1 Like

OK thank you.