0
\$\begingroup\$

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:

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);

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);
\$\endgroup\$
3
  • \$\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\$
    – user35344
    Oct 20, 2016 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, 2016 at 15:02
  • 1
    \$\begingroup\$ @Tyyppi_77 Good catch! \$\endgroup\$
    – Vaillancourt
    Oct 20, 2016 at 15:12

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .