Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
@Cioscos

This comment has been minimized.

Copy link

commented Feb 7, 2018

Hi! Can you tell me why this error comes out?
https://imgur.com/UCnTFl0

@itgenie98

This comment has been minimized.

Copy link

commented Feb 23, 2018

It is an bug that occurs in version 2.0.2. Downgrade to version 2.0.0 will help.

@phil123456

This comment has been minimized.

Copy link

commented Mar 29, 2018

I keep getting "warning uncorrect audio format " while I tried many settings

//Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096);
//Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
if (Mix_OpenAudio(44100, AUDIO_S32SYS, 2, 1024) == 1)

my mp3 load fine under windows and in audacity/vlc...

why is it up to the programmer to make the mp3 file fit the library ??? why do I have to guess and adapt my code depending on each mp3 format ? the playing format and the file format should be separated entities made compatible by the engine
what if I have different formats ? this makes no sens at all

I converted the file to OGG with the same settings and it works, mp3 codecs must be broken

@Eleobert

This comment has been minimized.

Copy link

commented Jun 9, 2018

Should not you call Mix_Quit () ??

@bachp2

This comment has been minimized.

Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.