Answer the question
In order to leave comments, you need to log in
How to make SDL2_mixer play mp3 format?
There is a Qt application in which I used SDL2_mixer to play music.
SDL2_mixer plays almost any format very quickly, unlike QMediaPlayer which still thinks before starting playback (the delay is due to the animator, which plays the animation for 10000 rendered objects). But the mp3 format, whatever one may say, does not want to be played.
Interestingly, even when either smpeg2.dll (on the Windows version) is in place and running, and no errors occur with either the Mix_Music * pointer, Mix_LoadMUS() loads the file and Mix_PlayMusic(play_mus, -1) returns 0, in columns silence.
- The paths to the files are absolute, in the format "/path/to/file.mp3" and "C:/path/to/file.mp3" (I get the paths through QFileInfo)
- ALL other formats are played, (Tested OGG, FLAC, WAV, MIDI).
Here are the pieces of code that are responsible for the operation of the SDL player:
In the main() function
SDL_Init(SDL_INIT_AUDIO);
int iniFlags = Mix_Init(MIX_INIT_FLAC | MIX_INIT_MOD | MIX_INIT_MP3 | MIX_INIT_OGG );
Mix_OpenAudio(44100, AUDIO_S16, 2, 4096);
namespace xxxxxxx
{
Mix_Music *play_mus = NULL;
void MUS_stopMusic()
{
Mix_HaltMusic();
}
void MUS_playMusic()
{
if(play_mus)
{
if(Mix_PlayMusic(play_mus, -1)==-1)
{
qDebug() << QString("Mix_PlayMusic: %1\n").arg(Mix_GetError());
// well, there's no music, but most games don't break without music...
}
}
else
{
qDebug() << QString("Play nothing: Mix_PlayMusic: %1\n").arg(Mix_GetError());
}
}
void MUS_changeVolume(int volume)
{
Mix_VolumeMusic(volume);
}
void MUS_openFile(QString musFile)
{
if(play_mus!=NULL) {Mix_FreeMusic(play_mus);play_mus=NULL;}
play_mus = Mix_LoadMUS( musFile.toLocal8Bit() );
if(!play_mus) {
qDebug() << QString("Mix_LoadMUS(\"%1\"): %2").arg(musFile).arg(Mix_GetError());
}
qDebug() << "Pointer is " << static_cast<void*>(&play_mus);
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question