Skip to content

Instantly share code, notes, and snippets.

@armornick
Created August 28, 2012 10:40
Show Gist options
  • Save armornick/3497064 to your computer and use it in GitHub Desktop.
Save armornick/3497064 to your computer and use it in GitHub Desktop.
Using SDL_Mixer with SDL2
#include <SDL2/SDL.h>
#include <SDL2/SDL_mixer.h>
#define WAV_PATH "Roland-GR-1-Trumpet-C5.wav"
#define MUS_PATH "HR2_Friska.ogg"
// Our wave file
Mix_Chunk *wave = NULL;
// Our music file
Mix_Music *music = NULL;
int main(int argc, char* argv[]){
// Initialize SDL.
if (SDL_Init(SDL_INIT_AUDIO) < 0)
return -1;
//Initialize SDL_mixer
if( Mix_OpenAudio( 22050, MIX_DEFAULT_FORMAT, 2, 4096 ) == -1 )
return -1;
// Load our sound effect
wave = Mix_LoadWAV(WAV_PATH);
if (wave == NULL)
return -1;
// Load our music
music = Mix_LoadMUS(MUS_PATH);
if (music == NULL)
return -1;
if ( Mix_PlayChannel(-1, wave, 0) == -1 )
return -1;
if ( Mix_PlayMusic( music, -1) == -1 )
return -1;
while ( Mix_PlayingMusic() ) ;
// clean up our resources
Mix_FreeChunk(wave);
Mix_FreeMusic(music);
// quit SDL_mixer
Mix_CloseAudio();
return 0;
}
@LaughingSun
Copy link

The answer is now unsupported but you can migrate. I mean to say they are 2 different APIs that server the same function but are not compatible. So migration is in the works. The good news is that they have the same capabilities.

Audio CDs (from SDK2 nigritory guide, https://wiki.libsdl.org/MigrationGuide?highlight=%28mixer%29)

The 1.2 CD API is completely gone. There's no replacement. Chances are you aren't shipping your music as CD-Audio tracks on a disc at this point, if you're shipping a disc at all. You can use Ogg Vorbis or some other audio file format for music, many of which are provided by SDL_mixer.

The official extensions SDL_mixer have a version dedicated to SDL 2.0 :==> SDL2_mixer. You may need to download them from the mercurial repositories for the latest fixes. Subsequently, of course, you will have to link e.g. SDL2_mixer, not SDL_mixture, to compile your program.

however the mix_* have gone the way of the dinosaurs.

@LaughingSun
Copy link

yeah I thought that wasn't right but I got it straight from the SDL2 official migratory page. its SDL2/SDL_Mixer, not SDL2/SDL2_mixer.

Btw that is something to keep in mind: it is best to use the parent directory in the path: #include <SDL2/$headername> because some of these headers such as SDL.h can live in the main include directories and it all looks like its ok, but it isn't: wrong header to library (1.2 header with 2 library), and then the link errors will be only a handful but it will haunt you and you may loose sleep over it.

Even more funny is that after they say it is obsolete it says this:
Audio
SDL 2.0 is completely compatible with the old API. But I think you should look for extensions, since the new API is awesome.

Which is only to say the SDL_mixer methods are the same (except for the mix_*) stuff, which is very much a part of it if you are starting from SDL1.2.

@aullidolunar
Copy link

Hello:

How can I use in Mix_OpenAudio the propper values of given ogg file loaded by the user?

@frranck
Copy link

frranck commented Jan 9, 2017

Thanks for the example !

@erikyuzwa
Copy link

recommend also calling Mix_Quit(); after Mix_CloseAudio();

@alpheratz0
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment