Advertisement

how to play background sound?

Started by January 25, 2005 02:08 AM
6 comments, last by kr_rin 19 years, 7 months ago
Hi, Anybody can tell me how to play background sound? I've looked at lesson 21 and several other tutorials, but using the PlaySound() function, I can only play one sound at a time. Can I play more than one sound at a time? Thanks
Background playback

If you want PlaySound() to return immediately (so that your program can go on to do other things) while Windows loads/plays the file in the background, then also specify the SND_ASYNC flag.

Here I play the file C:\WINDOWS\CHORD.WAV in the background:

if (!PlaySound("C:\\WINDOWS\\CHORD.WAV", 0, SND_ASYNC | SND_FILENAME))
{
printf("The sound didn't play!");
}

/* Now I can do other things while that waveform is playing */

Although you may wanna investigate FMOD so you can use different channels etc, let me know and I can fire you some code if you want it.
Advertisement
Actually what I mean is I want to play more than one sound.
I want to put a background music in my game that will loop until the game ends, as well as special effect sounds.

But using PlaySound("C:\\WINDOWS\\CHORD.WAV", 0, SND_ASYNC | SND_FILENAME), I can only play one sound at a time. I've tried SND_LOOP but the sound stops if I play another sound. I also tried SND_NOSTOP but it doesn't work as well.

Do I need to use different channel?

Thanks.
goto www.fmod.org and download the fmodapi374win.zip file and extract it into a directory of your choice.The fmod.dll\fmod.h\fmod_errors.h\fmodvc.lib are the files you will need, the rest are examplessetup the library at the start of your say main.cpp//=================================================================================#pragma comment(lib, "fmodvc.lib")  // link to fmod liberary file (for visual c++)#include <conio.h>#include <stdlib.h>#include <stdio.h>#include <time.h>#include <iostream>		using namespace std;#include "fmod.h"#include "fmod_errors.h"//=================================================================================// Samples to playFSOUND_SAMPLE *Sound_To_Play_1 = NULL;FMUSIC_MODULE *mod = NULL;void PlayMusic()									// Here's Where We Do the music{	// init	FSOUND_Init(44100, 64, 0);	// load song	mod = FMUSIC_LoadSong("data/music/revert.s3m");	// play song	FMUSIC_PlaySong(mod); }bool Play_Sound(long Channel, int Sound, int Mode, int Loaded)	// We play a sound{	if (Sound==EXTRA_COIN)	{		// Sound to play		Sound_To_Play_1 = FSOUND_Sample_Load(2,"data/sounds/coin.mp3",FSOUND_STEREO, 0);		// We play the sound		FSOUND_PlaySound(Channel,Sound_To_Play_1);	}	if (Sound==EXTRA_NONE)	{		// Sound to play		Sound_To_Play_2 = FSOUND_Sample_Load(2,"data/sounds/chinese.mp3",FSOUND_STEREO, 0);		// We play the sound		FSOUND_PlaySound(Channel,Sound_To_Play_2);	}	if (Sound==EXTRA_KEY)	{		// Sound to play		Sound_To_Play_3 = FSOUND_Sample_Load(2,"data/sounds/key.mp3",FSOUND_STEREO, 0);		// We play the sound		FSOUND_PlaySound(Channel,Sound_To_Play_3);	}	return true;}and at the end clear it all up	//free song and close	FMUSIC_FreeSong(mod);	FSOUND_Close();put this all in the file, note the music and the sounds are different ie channels etcthere was also a demo on gameturorials.com under sound, hope all thsi helps
Hey, thanks. I finally have nice background music for my game :D
But I found that the file type can be played for each function is limited.
FMUSIC can only play .MOD, .S3M, .XM, .IT or .MID
FSOUND can only play .WAV, .MP2, .MP3, .OGG or .RAW
Anyway, I can handle this.

^_^
FMUSIC is only for sequenced music types while FSOUND is for sampled music types. Chances are unless you are going to be using a midi file for your background music that you arent ever going to touch FMUSIC.

Within FSOUND there are 2 styles. Samples and Streams. Generally you are going to use samples for all of your sound effects as they dont take up much memory and can easyily be loaded on startup where as something like background music will probably be much larger and will be better off running as a stream.

Firelight's documentation for FMOD is awesome material so you shouldnt have any problems getting the stuff working.

8^)
Hard work USUALLY pays off in the future, but laziness ALWAYS pays off right now.
Advertisement
Can i change the sound of already playing PlaySound().

I.m playing music with playsound and i need user to control volume.
I've tryed
waveOutSetVolume(WAVE_MAPPER,0xAnyVALUE);

but it sets the soound of the system volume? not only my file.
You can use FMUSIC_SetMasterVolume.
It only controls volume of the music U play using FSOUND.
In the example below it only controls volume of mod.

// init
FSOUND_Init(44100, 64, 0);
// load song
mod = FMUSIC_LoadSong("data/backgroundmusic.mid");
// play song
FMUSIC_PlaySong(mod);

FMUSIC_SetMasterVolume(mod, 16);


You can see the help file, they're quite useful.

This topic is closed to new replies.

Advertisement