Jump to content
  • Advertisement
Sign in to follow this  

Sound in ur game

This topic is 4611 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering what is the simplest and the easier way to get some sounds in my game? Pls suggest some simple library or sum like that. I wanna an mp3 playing in background and some wav playing sometimes too. Ty.

Share this post

Link to post
Share on other sites
I used the fmod library the first time I wanted sound in my game and had it working within a matter of minutes. It's simply a dll that sits alongside your app and is initialised with a few calls and sounds are cached and played via a simple selection of calls. Caused me no problems whatsoever.

Share this post

Link to post
Share on other sites
I am using SDL_Mixer for my game, it is really easy to use.

Here is all you need to play the music:

#include <stdio.h>
#include <stdlib.h>
#include "SDL.h"
#include "SDL_mixer.h"

/* Mix_Music actually holds the music information. */
Mix_Music *music = NULL;

void handleKey(SDL_KeyboardEvent key);
void musicDone();

int main(void) {

SDL_Surface *screen;
SDL_Event event;
int done = 0;

/* We're going to be requesting certain things from our audio
device, so we set them up beforehand */

int audio_rate = 22050;
Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
int audio_channels = 2;
int audio_buffers = 4096;


/* This is where we open up our audio device. Mix_OpenAudio takes
as its parameters the audio format we'd /like/ to have. */

if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
printf("Unable to open audio!\n");

/* If we actually care about what we got, we can ask here. In this
program we don't, but I'm showing the function call here anyway
in case we'd want to know later. */

Mix_QuerySpec(&audio_rate, &audio_format, &audio_channels);

/* We're going to be using a window onscreen to register keypresses
in. We don't really care what it has in it, since we're not
doing graphics, so we'll just throw something up there. */

screen = SDL_SetVideoMode(320, 240, 0, 0);

while(!done) {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
done = 1;

/* So we don't hog the CPU */


/* This is the cleaning up part */


void handleKey(SDL_KeyboardEvent key) {
switch(key.keysym.sym) {
case SDLK_m:
if(key.state == SDL_PRESSED) {

/* Here we're going to have the 'm' key toggle the music on and
off. When it's on, it'll be loaded and 'music' will point to
something valid. If it's off, music will be NULL. */

if(music == NULL) {

/* Actually loads up the music */
music = Mix_LoadMUS("music.ogg");

/* This begins playing the music - the first argument is a
pointer to Mix_Music structure, and the second is how many
times you want it to loop (use -1 for infinite, and 0 to
have it just play once) */

Mix_PlayMusic(music, 0);

/* We want to know when our music has stopped playing so we
can free it up and set 'music' back to NULL. SDL_Mixer
provides us with a callback routine we can use to do
exactly that */


} else {
/* Stop the music from playing */

/* Unload the music from memory, since we don't need it
anymore */


music = NULL;

/* This is the function that we told SDL_Mixer to call when the music
was finished. In our case, we're going to simply unload the music
as though the player wanted it stopped. In other applications, a
different music file might be loaded and played. */

void musicDone() {
music = NULL;

P.S. SDL is also required for this

Share this post

Link to post
Share on other sites
there is a excellent article here on gamedev.net about mp3 playback with the MCI

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!