I am trying to get music to loop in the background of a game. However im really not sure how to do this. Can anyone give me a starting point please? I am using Visual C++ btw.

Dani AI

Generated

Nice job getting a loop working — ’s follow-up shows the Win32 route worked and pointed you toward the platform API. A few practical notes that help when you move beyond a quick test or run into problems in a real game.

PlaySound / winmm is the simplest path for a short WAV loop, but it has limits: it’s best for small uncompressed clips (it typically loads the whole file into memory), and it isn’t ideal for long music tracks, compressed formats, or low-latency mixing. For anything beyond a single short loop (streaming background music, cue points, volume control, multiple tracks, gapless transitions) consider an audio library or a more modern API.

Common, practical alternatives:

  • SDL2 + SDL_mixer or SFML.Audio — easy C++ integration and cross‑platform support for OGG/WAV and simple streaming.
  • XAudio2 or OpenAL (or DirectSound on older Windows) — lower‑level, good for low latency and mixing, but more code to manage.
  • FMOD or BASS — full-featured engines with codecs and streaming; check licensing for commercial use.

If you need a quick way to loop MP3s on Windows without additional libs, the MCI interface can loop with a single command. Example (Win32):

#include <windows.h>

// open and loop the file
mciSendStringA("open \"music.mp3\" type mpegvideo alias bg", NULL, 0, NULL);
mciSendStringA("play bg repeat", NULL, 0, NULL);

// later: stop and close
mciSendStringA("stop bg", NULL, 0, NULL);
mciSendStringA("close bg", NULL, 0, NULL);

Two extra tips: if you hear clicks/gaps at loop points, MP3 padding or resampling is a common cause — use PCM WAV or Ogg Vorbis for seamless loops or implement a small crossfade. And if you move to a game engine or need multiple sounds, pick a library that supports streaming and mixing rather than relying on the simple system calls.

Recommended Answers

All 3 Replies

If you are on windows, then you can use PlaySound . If not then there might be something similar to your
OS. If all fails, you will have to use a 3rd party library.

I got this piece of code

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#pragma comment(lib,"Winmm.lib")

int main(void)
{
	PlaySound("file.wav",NULL,SND_FILENAME);

	return 0;
}

But it will only play at the begining of the program where i want it to loop- how can i do this?

I got it to work by using this code

PlaySound("beep.wav",NULL,SND_FILENAME|SND_ASYNC | SND_LOOP);

Thanks for your help

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.