0
void LoadMusic(string path);

Mix_Music* gMusic = NULL;


        LoadMusic("Music/bubble-bobble.mp3");
        if(Mix_PlayingMusic() == 0)
        {
            Mix_PlayMusic(gMusic, -1);
        }

    if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
    {
        cout << "Mixer could not initialise. error: " << Mix_GetError();
        return false;
    }


    Mix_FreeMusic(gMusic);
    gMusic = NULL;

void LoadMusic(string path)
{
    gMusic = Mix_LoadMUS(path.c_str());

    if(gMusic == NULL)
    {
        cout << "Failed to load background music! Error: " << Mix_GetError() << endl;
    }
}

Been following a tutorial on how to get audio to work with my game, think something has gone wrong somewhere as it's not playing any sound at all. Not 100% where this is going wrong but does anyone have an idea what I have done wrong and how to fix it?

JangGwang
  • 19
  • 3
  • 1
    There's a lot of unrelated code in your program, at least unrelated to your problem and this question. Please make a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) and show us instead. – Some programmer dude Apr 09 '14 at 10:56
  • Took out the code that's not needed. If you need the full code it's here http://pastebin.com/MdQuHnjt. It might be that I have the code the in the wrong places – JangGwang Apr 09 '14 at 11:10
  • very simple audio player in c++ : https://github.com/abdullahfarwees/Wav_Audio_player_SDL – Abdullah Farweez Jul 18 '18 at 05:38

2 Answers2

3

You don't specify your operating system and other important context, but...

If you are running your build under Windows, you might have encountered the same problem as I did:

Surprisingly, the SDL library requires an environment variable in order to play audio at all.

Try adding SDL_AUDIODRIVER=waveout (or alternatively, SDL_AUDIODRIVER=dsound) to your environment (under Windows).

AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
Jim Davis
  • 41
  • 3
  • 2
    Or, if you're on SDL2 on Windows, you'll need `SDL_AUDIODRIVER=directsound` or `SDL_AUDIODRIVER=winmm` – benjymous Mar 02 '18 at 15:07
0

I had a similar problem, was following a tutorial to create my own C++ simple audio player, and it wasn't working. I made it display the names of the available playback devices, but none of them seemed to work, until I added the environment variable as stated here.

It worked fine in Windows 10, after adding to my system an environment Variable named SDL_AUDIODRIVER=directsound .

alex01011
  • 1,670
  • 2
  • 5
  • 17