Skip to content

Instantly share code, notes, and snippets.

@armornick
Created August 28, 2012 10:40
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • 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

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