SFML community forums
Help => Audio => Topic started by: WrathfulProgrammer on March 21, 2010, 02:26:44 pm
-
Hello, for some reason my sounds stop after a while, music continues playing as normal though.
I suspect it could have something to do with the frequency of the sound instance creation(Quite often), could it have something to do with that?
Init, called once on init.
if (!Music.OpenFromFile("DJYeoh0.ogg"))
{
MessageBox(hWnd,"Failed to load DJYeoh0.ogg.","Warning",MB_ICONEXCLAMATION | MB_OK);
return false;
}
if (!Buffer.LoadFromFile("laser0.ogg"))
{
MessageBox(hWnd,"Failure to load from file.","Warning",MB_ICONEXCLAMATION | MB_OK);
return false;
}
Create, this gets called each time a unit shoots/dies.
sf::Sound *temp_sound = new sf::Sound;
temp_sound->SetBuffer(ps->app->Buffer);
ps->sound_vec.push_back(temp_sound);
temp_sound->SetPitch(3);
temp_sound->Play();
Update called each frame
std::vector<sf::Sound*>::iterator sound_it = sound_vec.begin();
while(sound_it != sound_vec.end()){
int duration = (*sound_it)->GetBuffer()->GetDuration();
int elapsed = (*sound_it)->GetPlayingOffset();
if(elapsed >= duration){
sound_it = sound_vec.erase(sound_it);
}
else
sound_it++;
}
Thanks.
-
You should keep track of the number of sounds that are alive, and tell me the maximum value that you get. You cannot create more than a given number of sound sources (this is a limitation of OpenAL), so this may explain your problem.
-
My apologies, found the problem, I wasn't deleting the instances.
-
Im using SFML v1.5
I have the same problem... and i try delete instace but.. SFML fail.. :\ (me fail but... xDDD)
I have this:
std::deque<sf::Sound*>::iterator it = sounds.begin();
while(it != sounds.end())
{
const sf::SoundBuffer *sbuffer = (*it)->GetBuffer();
float duration = sbuffer->GetDuration();
float pos = (*it)->GetPlayingOffset();
if (pos == duration)
{
delete((*it));
(*it) = 0x0;
it = sounds.erase(it);
}
else
++it;
}
In this code.... "pos" never is == to "duration" :S
std::deque<sf::Sound*>::iterator it = sounds.begin();
while(it != sounds.end())
{
if ((*it)->GetStatus() == sf::Sound::Stopped)
{
delete((*it));
(*it) = 0x0;
it = sounds.erase(it);
}
else
++it;
}
This code delete the sound more fast... (NO LISTEN SOUND :S)
I add new sound with:
void CEngine::playSound(int sound_type)
{
sf::Sound *sound;
switch(sound_type)
{
case SOUND_FIRE:
{
sound = new sf::Sound(snd_fire01);
} break;
case SOUND_IMPACT:
{
sound = new sf::Sound(snd_impact01);
} break;
case SOUND_JUMP:
{
sound = new sf::Sound(snd_jump);
} break;
case SOUND_HURT:
{
sound = new sf::Sound(snd_hurt01);
} break;
case SOUND_FX1:
{
sound = new sf::Sound(snd_fx01);
} break;
default:
return;
}
sound->Play();
sounds.push_back(sound);
}
Help me pls!
-
pls... anybody can help me??? ¿why? ¿dirty code? ¿tutorial solution? ¿...?
-
As always, a minimal code (http://www.sfml-dev.org/wiki/en/faq#what_is_a_minimal_code) could be helpful.
And try to explain your problem clearly, I didn't understand if your sound is playing or not. And maybe you can count yur numbe of sounds in order to answer to Laurent ;)