0

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();
}
Simian
  • 1
  • 3
  • I'm not quite sure how SDL works in this regard. But, if you consider "looping" as the number of times to play the sound in total, then "looping == 1" resulting in one playthrough of the sound seems fairly reasonable (assuming that 2 results in a total of two playthroughs)... – Jesper Juhl Jun 07 '17 at 19:07
  • Thanks for that, I'll keep that noted! But shouldn't this still play the song at least once? Because it's not playing all? :( And I know my file works, because as I said it's an intermittent issue. I restarted my machine and it works the first couple of times then stops, so I'm guessing I have a memory issue somewhere – Simian Jun 07 '17 at 19:14
  • Can you show us `main()`? – Exercise To The Reader Jun 10 '17 at 02:03

0 Answers0