I'm using Microsoft Visual C++ 2008. How would i play a sound file? Either .wav, .mp3, or .midi hopefully.

If you want a good deal of control over the sound, you might want to consider DirectSound (though using it can be very annoying and complex). If not, I believe there is a simple Windows function that you can use to play sounds asynchronously, but I can't remember it on the spot.

SDL_mixer program playing music.mp3

#include <SDL/SDL_mixer.h>

int main(int argc, char **argv) {
	setbuf(stdout, 0);
	setbuf(stderr, 0);

	if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096) != 0) {
		fprintf(stdout, "Unable to initialize audio: %s\n", Mix_GetError());
		exit(1);
	}

	Mix_Music *music = Mix_LoadMUS("music.mp3");
	if (!music) {
		printf("Mix_LoadMUS(\"music.mp3\"): %s\n", Mix_GetError());
		exit(1);
	}

	if (Mix_PlayMusic(music, -1)==-1) {
		printf("Mix_PlayMusic: %s\n", Mix_GetError());
		exit(1);
	}

	system("PAUSE");
	return 0;
}

I really need the Windows function to play it.

Doesn't seem to work. Umm... i guess what i'm really looking for is an example to play a sound file while the user is going through the program and an example that would stop the sound.

You don't need any library at all (avoid SDL, for kids only)
Just use win32 MM native apis.

Doesn't seem to work. Umm... i guess what i'm really looking for is an example to play a sound file while the user is going through the program and an example that would stop the sound.

Maybe you tried it the wrong way, the following plays a .wav file

// start playing the file continuosly and asynchronously
PlaySound("C:\\WINNT\\Media\\Ctmelody.wav", 0, SND_FILENAME|SND_LOOP|SND_ASYNC);
// Listen to it for 5 secs
Sleep(5000);
// Stop playing
PlaySound(0,0,0);

Doesn't seem to work. Umm... i guess what i'm really looking for is an example to play a sound file while the user is going through the program and an example that would stop the sound.

Did you want to loop the sound? That still may be possible with the PlaySound() function, but it would depend on the structure of your program. Is it loop based? (i.e., there is some initialization code, then the program goes into a loop where it continually executes some code until either the user chooses to exit, or the program exits for some other reason).

Maybe you tried it the wrong way, the following plays a .wav file

// start playing the file continuosly and asynchronously
PlaySound("C:\\WINNT\\Media\\Ctmelody.wav", 0, SND_FILENAME|SND_LOOP|SND_ASYNC);
// Listen to it for 5 secs
Sleep(5000);
// Stop playing
PlaySound(0,0,0);

Well, it says "Library: Use Winmm.lib." How would i do that?

You need to link to that lib. Easiest way would be to add:

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

somewhere in your code.

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.