Skip to content

Instantly share code, notes, and snippets.

@cdave1
Created April 13, 2014 00:21
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save cdave1/10563386 to your computer and use it in GitHub Desktop.
Save cdave1/10563386 to your computer and use it in GitHub Desktop.
Play an mp3 with SDL2
#include "SDL2/SDL.h"
#include "SDL2/SDL_mixer.h"
static const char *MY_COOL_MP3 = "cool_tunes.mp3";
int main(int argc, char **argv) {
int result = 0;
int flags = MIX_INIT_MP3;
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
printf("Failed to init SDL\n");
exit(1);
}
if (flags != (result = Mix_Init(flags))) {
printf("Could not initialize mixer (result: %d).\n", result);
printf("Mix_Init: %s\n", Mix_GetError());
exit(1);
}
Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640);
Mix_Music *music = Mix_LoadMUS(MY_COOL_MP3);
Mix_PlayMusic(music, 1);
while (!SDL_QuitRequested()) {
SDL_Delay(250);
}
Mix_FreeMusic(music);
SDL_Quit();
return 0;
}
@Eleobert
Copy link

Eleobert commented Jun 9, 2018

Should not you call Mix_Quit () ??

@bachp2
Copy link

bachp2 commented Sep 3, 2018

Hi all,
This bug is conclusively linked to calling Mix_Init before Mix_OpenAudio as detailed here:
https://discourse.libsdl.org/t/bug-sdl2-mixer-init-error-format-support-not-available/23705/2
Reverse the order of calls solved my problem

@singalen
Copy link

I assume SDL_mixer doesn't support iOS' hardware mp3 decoding?

@nonetrix
Copy link

it would be best to get the sample rate best way is to read the first few bytes of the file pretty sure if it is incorrect it can sound slow or too fast

@Kitsune64
Copy link

That's don't work here, don't know why. But I can compile it.

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