Playing mp3/ogg with SDL_Mixer

Hope this is the correct place to ask.

I am trying to load a ogg-file for music (alternatively a mp3-file). My code looks like this:

int result;

auto mixinitflags = MIX_INIT_OGG | MIX_INIT_MP3;
if (mixinitflags != (result = Mix_Init(mixinitflags))) {
printf(“Could not initialize mixer (result: %d).\n”, result);
printf(“Mix_Init: %s\n”, Mix_GetError());
exit(1);
}
assert(0 == Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 512));
mp3music = Mix_LoadMUS(“res/music.ogg”);
if (!mp3music)
{
std::cerr << "Failed to load music. " << Mix_GetError() << std::endl;
exit(2);
}
if (Mix_PlayMusic(mp3music, -1) == -1) {
printf(“Mix_PlayMusic: %s\n”, Mix_GetError());
exit(3);
}

I use cmake and vcpkg and it works on Mac, but not on Windows. Vcpkg doesnt copy the decoder-libraries so I had to download and copy them to the output-folder by hand (otherwise I get “OGG support not available”). When I do this get “Failed to load music. Unrecognized audio format”. But as I said, only on Windows and not Mac. I’ve tried playing both a ogg and mp3 version and downloaded random ogg-files from the internet to try out but nothing works. The files do play in any other player on Windows (VLC etc).

I have run out of ideas, would appreciate any help immensely!

Since you use vcpkg, you have to install additional “extension” libraries (they are just SDL2_Mixer with more files support). All SDL2_mixer-related vcpkg packages are:

sdl2-mixer:x64-windows                             2.0.4-9          Multi-channel audio mixer library for SDL.
sdl2-mixer[dynamic-load]:x64-windows                                Load plugins with dynamic call
sdl2-mixer[libflac]:x64-windows                                     Support for FLAC audio format.
sdl2-mixer[libmodplug]:x64-windows                                  Support for MOD audio format.
sdl2-mixer[libvorbis]:x64-windows                                   Support for OGG Vorbis audio format.
sdl2-mixer[mpg123]:x64-windows                                      Support for MP3 audio format.
sdl2-mixer[opusfile]:x64-windows                                    Support for Opus audio format.

Just install the ones you need, then try to build your code. It worked for me.

Hi TrnSTrA, I follow your method installed these packages.
$ ./vcpkg.exe search sdl2-mixer
sdl2-mixer 2.0.4-9 Multi-channel audio mixer library for SDL.
sdl2-mixer[dynamic-load] Load plugins with dynamic call
sdl2-mixer[libflac] Support for FLAC audio format.
sdl2-mixer[mpg123] Support for MP3 audio format.
sdl2-mixer[libmodplug] Support for MOD audio format.
sdl2-mixer[libvorbis] Support for OGG Vorbis audio format.
sdl2-mixer[opusfile] Support for Opus audio format.

But I can not run my exe file. I got this error message from SDL
Failed loading vorbisfile.dll
After I copy vorbisfile.dll into my exe folder I still get this problem.
Can I ask you how to fix this problem? THX!
Environment: win10 visual studio 2019 cmake

Hi @elfdroid . vcpkg search shows all the available vcpkg packages that match the name you search ( on your case it shows all the packages that have sdl2-mixer on their name). That means the error shows because you haven’t installed any “extension” packages. Since it complains about vorbisfile.dll, you need to run:

./vcpkg.exe install sdl2-mixer[libvorbis]

Then retry to build/run your .exe. Hope it works for you. If it doesn’t, please reply my comment.

Thanks for your help!
I have installed these packages,
sdl2-mixer:x86-windows
sdl2-mixer[dynamic-load]:x64-windows
sdl2-mixer[dynamic-load]:x86-windows
sdl2-mixer[libflac]:x86-windows
sdl2-mixer[libmodplug]:x86-windows
sdl2-mixer[libvorbis]:x64-windows
sdl2-mixer[libvorbis]:x86-windows
sdl2-mixer[mpg123]:x86-windows
sdl2-mixer[opusfile]:x86-windows

And these are my cmake files:
cmake_minimum_required (VERSION 3.8)
project (“SDL2Demo”)

set(CMAKE_CXX_STANDARD 17)

find_package(SDL2 CONFIG REQUIRED)
find_package(sdl2-image CONFIG REQUIRED)
find_package(sdl2-ttf CONFIG REQUIRED)
find_package(sdl2-mixer CONFIG REQUIRED)
include_directories(Source)

add_subdirectory(Source)
set(BINARY ${CMAKE_PROJECT_NAME})

add_executable(${BINARY}_run main.cpp)
target_link_libraries(${BINARY}_run PRIVATE SDL2::SDL2 SDL2::SDL2main SDL2::SDL2_image SDL2::SDL2_ttf SDL2::SDL2_mixer)

Sorry I didn’t make it clear. The problem still exists.
My source code:

Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 2048);
int flags = MIX_INIT_OGG | MIX_INIT_MOD;
int initted = Mix_Init(flags);
if ((initted & flags) != flags) {
printf(“Mix_Init: Failed to init required ogg and mod support!\n”);
printf(“Mix_Init: %s\n”, Mix_GetError());
// handle error
}
Mix_Music* song = NULL;
song = Mix_LoadMUS(“Assets/cave.ogg”);
if (!song)
{
SDL_Log(“Load music file failed! %s”, Mix_GetError());
return -1;
}

I get these error messages:

Mix_Init: Failed to init required ogg and mod support!
Mix_Init: OGG support not available
INFO: Load music file failed! Failed loading vorbisfile.dll

It was my bad too. So, I found the problem. You see, SDL2 has a cross-platform API for loading .dll/.so, etc (dynamic libraries), which it uses for loading vorbisfile.dll, etc. The error Failed loading vorbisfile.dll is registered because LoadLibrary (a Win32 function called by SDL2) failed. Probably sdl2-mixer[libvorbis] wasn’t installed corectly. Please check (YOUR_VCPKG_EXE_LOCATION)/installed/x64-windows/bin to see if vorbisfile.dll is located there. In case not, run ./vcpkg.exe remove sdl2-mixer[libvorbis] and then ./vcpkg.exe install sdl2-mixer[libvorbis]. Make sure to check if any error happens while the package gets installed. Hope this helps you.

Going back to this, I want to thanks for the reply. Never had it working though so I just switched Conan which worked out of the box. Pretty sure there is a bug with the vcpkg - sdl-mixer package since everything else worked without a hitch.

Thanks for reply! I will try your solution!