c++mp3sdl-2sdl-mixer

SDL_Mixer 2.0.4 "MP3 support not available" even though libmpg123 is installed


I am a beginner to programming and am trying to make a simple console MP3 player as a project. For this, I need a way to play .mp3 files. SDL Mixer provides this facility but requires libmpg123 installed on one's system.

Call to 'Mix_Init()' always fails even though I have the required dependency 'libmpg123' installed. I do not have the dependencies for other formats i.e., FLAC, OGG, MOD and MIDI if that matters.

I am running Ubuntu 18.04 on my laptop and I have SDL2 version 2.0.9, SDL_Mixer 2.0.4 both built from source (although I have tried the versions available through the apt package manager). I also have libmpg123 installed which I have also built from source (again, I have also tried the version available through the package manager).

I compiled the code in two ways:

g++ Mix_Init.cpp -lSDL2 -lSDL2_mixer

and

g++ Mix_Init.cpp -lSDL2 -lSDL2_mixer -lmpg123

I don't know which one is right but neither one fixes the problem.

#include<SDL2/SDL.h>
#include<SDL2/SDL_mixer.h>
#include<iostream>

int main(int argc, char** argv)
{
    if(SDL_Init(SDL_INIT_AUDIO))
        std::cerr << "SDL_Init() Error: " << SDL_GetError() << std::endl;

    else {
        int result {0};
        if(!((result = Mix_Init(MIX_INIT_MP3)) && MIX_INIT_MP3)){
            std::cerr << Mix_GetError() << std::endl;
            std::cerr << "Mix_Init() returns " << result << std::endl;
        }
        else {
            std::cout << "Success!" << std::endl;
        }
    }
    return 0;
}

Here are the contents of my /usr/local/lib directory:

cmake           libmpg123.so.0.44.8  libSDL2-2.0.so.0      libSDL2main.la              libSDL2_mixer.so  pkgconfig
libglfw3.a      libout123.la         libSDL2-2.0.so.0.9.0  libSDL2_mixer-2.0.so.0      libSDL2.so        python2.7
libmpg123.la    libout123.so         libSDL2.a             libSDL2_mixer-2.0.so.0.2.2  libSDL2_test.a    python3.6
libmpg123.so    libout123.so.0       libSDL2.la            libSDL2_mixer.a             libSDL2_test.la   python3.7
libmpg123.so.0  libout123.so.0.2.2   libSDL2main.a         libSDL2_mixer.la            mpg123

I expect the output:

Success!

Instead I get:

MP3 support not available
Mix_Init() returns 0

UPDATE:

Apparently, I have the other dependencies installed as well...

But I have no idea how to use them.

enter image description here


Solution

  • You need to have development files for mpg123 installed when you're building SDL2_mixer. E.g. for debian/ubuntu you'll need libmpg123-dev (and likewise for other formats), and SDL_mixer's ./configure should output something like

    checking mpg123.h usability... yes
    checking mpg123.h presence... yes
    checking for mpg123.h... yes
    checking for mpg123_replace_reader_handle in -lmpg123... yes
    -- dynamic libmpg123 -> libmpg123.so.0
    

    Then make && make install. Or use distro-provided libsdl2-mixer-dev, if it fits your requirements.