PLaying a sound file with SDL - C++


 
Thread Tools Search this Thread
Top Forums Programming PLaying a sound file with SDL - C++
# 1  
Old 08-30-2010
Question 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:
Code:
#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....Smilie
# 2  
Old 08-30-2010
SDL_mixer is an additional thing to install. Try searching your package manager or see here.
This User Gave Thanks to JohnGraham For This Post:
# 3  
Old 08-30-2010
OK thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Programming

Playing Video with MinGW and SDL

Hi All, I was wondering if someone could clarify the best way to display mpeg video with SDL in MinGW. After scouring the internet it seems there is not a lot of information regarding this subject (or at least not up to date) and libraries that are available for this purpose. Any help/... (1 Reply)
Discussion started by: robfwauk
1 Replies

2. Programming

SDL Image Filtering

Solved thanks for info!! (8 Replies)
Discussion started by: aLHaNz
8 Replies

3. Red Hat

playing sound in rhel5.1 ?

please any body tell me how to play sound using RealPlayer11 loaded in Rhel5.1? (1 Reply)
Discussion started by: rangaprem
1 Replies

4. Ubuntu

Playing wav-file sounds.

I would like to be able to shell out to the command line and play sounds. After installing "sox" I can now do this using the play command. Is there a way to stop the playing once it starts? (5 Replies)
Discussion started by: newyorkpaulie
5 Replies

5. UNIX for Dummies Questions & Answers

SDL error with the version

hi, i've just downloaded a test game to try compiling it with the gcc compiler in Solaris, the problem was that ./configure command return this error *** Could not run SDL test program, checking why... *** The test program compiled, but did not run. This usually means *** that the run-time linker... (0 Replies)
Discussion started by: freeware
0 Replies

6. Programming

SDL doubt

Hi, I am working on SDL open-source API's on Debian Linux for my game development project. I am not sure whether posting SDL related queries here is correct. I would like to know what is the difference between: SDL_SetVideoMode() and SDL_VideoModeOK() functions (0 Replies)
Discussion started by: royalibrahim
0 Replies

7. UNIX for Dummies Questions & Answers

What is SDL?

I get a: Requirements: SDL when I'm browsing (www.happypenguin.org) for games. According to wikipedia SDL can be: where some are more likely than others. However, which one is it? I got an error because of SDL last time I tried and want to make it right before I try... (0 Replies)
Discussion started by: riwa
0 Replies

8. UNIX for Dummies Questions & Answers

Insane question : Playing mp3 file from Sun Sparc

hi, i know this is crazy. but i can do this from linux in intel platform. just wonder can my Sun Solaris 8 sparc can do the same thing? (2 Replies)
Discussion started by: champion
2 Replies
Login or Register to Ask a Question

Featured Tech Videos