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;
}
@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