I'm coding a game in SDL2 and I'm implementing the audio system using SDL_Mixer. However, my music file won't play during the program and I don't know why. How do I get the music to play?

Audio initialization:


Opening the audio device:

if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
    std::cout << "Error:" << Mix_GetError() << std::endl;

Loading the sound file:

Mix_Chunk *soundEffect = Mix_LoadWAV("Sound\BetaMusic.wav");

Playing the sound file:

Mix_PlayChannel(-1, soundEffect, -1);
  • \$\begingroup\$ Have you checked that the audio chunk loads correctly? Also, in general you should use the music-playing functionality that is provided by the library to play music. What happens if you change the path to "Sound/BetaMusic.wav"? \$\endgroup\$
    – Tyyppi_77
    Oct 20 '16 at 14:53
  • \$\begingroup\$ @Tyyppi_77 the slash was the wrong way, I got confused considering it's usually \ as opposed to /, thanks for helping me, if you convert your comment into an answer I can mark it as the answer \$\endgroup\$ Oct 20 '16 at 15:02
  • 1
    \$\begingroup\$ @Tyyppi_77 Good catch! \$\endgroup\$
    – Vaillancourt
    Oct 20 '16 at 15:12

You need to change the backslash into a forward slash while loading the audio chunk. Mix_Chunk *soundEffect = Mix_LoadWAV("Sound/BetaMusic.wav"); should do the trick.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.