I'm learning how to use the SDL libraries but I'm having a small problem playing the sound. The program appears to load the sound but either not play it or play it too low for me to hear. Here's my code so far:

#include "SDL/SDL.h"
#include "SDL/SDL_mixer.h"

Mix_Music *play_sound = NULL;

void cleanUp()
{
     Mix_FreeMusic(play_sound);
     Mix_CloseAudio();
     SDL_Quit();
}

int main(int argc, char* args[])
{
    SDL_Init(SDL_INIT_EVERYTHING);
    Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
    play_sound = Mix_LoadMUS("noise.mp3");
    Mix_PlayMusic(play_sound, -1);
    cleanUp();
    return 0;
}

I'm using Dev-Cpp with these linker arguments:
-lmingw32 -lSDLmain -lSDL -lSDL_mixer

and I have the proper .dll's in the folder with my project along with the actual .mp3. Any suggestions?

Dani AI

Generated

Good catch — the symptom here is that playback was started and the program immediately exited, so the mixer never had time to output sound. Keep the process alive while audio is playing. A minimal pattern is to poll the music state or run a small event loop; for example:

/* wait while music is being mixed */
while (Mix_PlayingMusic()) {
    SDL_Delay(100);
}

/* or a simple SDL event loop that lets the app quit cleanly */
SDL_Event e;
int running = 1;
while (running) {
    while (SDL_PollEvent(&e)) {
        if (e.type == SDL_QUIT) running = 0;
    }
    SDL_Delay(50);
}

Setting an explicit music volume can also help if output seems very quiet (Mix_VolumeMusic). (wiki.libsdl.org)

Make the code robust by checking return values and printing the mixer/SDL error strings. If the audio device fails to open or a music file fails to load, Mix_OpenAudio and Mix_LoadMUS will report failures that should be handled rather than ignored. If MP3 decoding is absent in the build of SDL_mixer being used, calling Mix_Init (or converting test files to OGG/WAV) will reveal or avoid codec problems. Also be careful with teardown order: free music resources before closing the audio subsystem to avoid crashes. ()

For seeing "There is no soundcard": that typically indicates the OS has no usable audio backend (common on headless VMs or missing drivers). On Linux, check available devices with tools like aplay -l / cat /proc/asound/cards, verify ALSA/PulseAudio/firmware and kernel modules, or try forcing SDL to another backend for testing:

SDL_AUDIODRIVER=alsa ./a.out
SDL_AUDIODRIVER=dummy ./a.out   # runs without real audio

The Arch/SDL troubleshooting notes explain setting SDL_AUDIODRIVER when SDL apps have no sound. (wiki.archlinux.org)

Final practical checks: test playback with a simple WAV/OGG file known to work, verify OS mixer/unmuted channels, confirm the mixer build actually lists the desired decoders (Mix_GetNumMusicDecoders / Mix_HasMusicDecoder), and print Mix_GetError() on failures to get actionable messages.

Forgot that I need time between playing the music and ending it haha!

this program got compiled correctly without any error and warning.
But while running this program with ./a.out, it is giving message as "There is no soundcard".
Can anyone help me for this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.