• Create Account

### #Actualmunchor

Posted 03 March 2012 - 04:49 AM

I'm using SDL_mixer to play a .wav sound, but I don't know when to free it.

I have this code:

Bullet::Bullet() {
//Create Bullet

/* Play the shot sound */
sound = Mix_LoadWAV("media/gunshot.wav"); //sound is a private property of Bullet

if (sound == NULL) {
fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
}

Mix_PlayChannel(0, sound, 0);
}


My problem is that if I free the sound right after I play it, it will not be played.
If I free the sound when the bullet is deleted (deconstructor), the sound stops before it is over, because I delete bullets whenever they leave the screen, and sometimes they leave the screen before the sound isn't over.

So, my doubt it, how can I handle loading the sound and freeing it without super RAM consumption? Thank you!

### #1munchor

Posted 03 March 2012 - 04:48 AM

I'm using SDL_mixer to play a .wav sound, but I don't know when to free it.

I have this code:

Bullet::Bullet() {
//Create Bullet

/* Play the shot sound */
sound = Mix_LoadWAV("media/gunshot.wav"); //sound is a private property of Bullet

if (sound == NULL) {
fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
}

Mix_PlayChannel(0, sound, 0);
}


My problem is that if I free the sound right after I play it, it will not be played.
If I free the sound when the bullet is deleted (deconstructor), the sound stops before it is over, because I delete bullets whenever they leave the screen, and sometimes they leave the screen before the sound isn't over.

So, my doubt it, how can I handle loading the sound and freeing it without super RAM consumption? Thank you!

PARTNERS