I'm currently working on a simple music player, however, I've hit a problem where I can loop music indefinitely or any number of times > 1, but if I try to play the song only once then nothing plays. Having searched here too for about 30 minutes I couldn't find a question documenting the same problem.
The weird part is this used to work, I saved after it worked, closed, opened, then it no longer worked. I had not changed anything.
I think it might be an issue with how I'm cleaning up SDL_Mixer, but after an hour of debugging I'm no closer to a solution.
#include <SDL_Mixer.h>
bool Program::_running = false;
void Program::Run()
{
if (_running)
{
return;
}
_running = true;
if (Mix_Init(MIX_INIT_MP3) == 0)
{
std::cout << Mix_GetError();
return;// false;
}
if (Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) < 0)
{
return;
}
_music = Mix_LoadMUS("Music/03 - Code Monkey.mp3");
if (_music == nullptr)
{
std::cout << Mix_GetError();
}
else
{
if (Mix_PlayMusic(_music, 1) < 0)
{
std::cout << Mix_GetError();
}
}
while (_running)
{
Update();
Render();
}
}
And here's my current clean-up:
Program::~Program()
{
Mix_FreeMusic(_music);
Mix_CloseAudio();
}