Game Development Reference
In-Depth Information
private int FindNextFreeChannel()
{
foreach (int slot in _soundChannels)
{
if (!IsChannelPlaying(slot))
{
return slot;
}
}
return -1;
}
This function finds a free channel for our sounds to be played on. This makes it
easy to write a robust PlaySound method that will be able to deal with a limited
number of sound channels.
public Sound PlaySound(string soundId)
{
// Default play sound doesn't loop.
return PlaySound(soundId, false);
}
public Sound PlaySound(string soundId, bool loop)
{
int channel = FindNextFreeChannel();
if (channel != -1)
{
Al.alSourceStop(channel);
Al.alSourcei(channel, Al.AL_BUFFER, _soundIdentifier[soundId].
_bufferId);
Al.alSourcef(channel, Al.AL_PITCH, 1.0f);
Al.alSourcef(channel, Al.AL_GAIN, 1.0f);
if (loop)
{
Al.alSourcei(channel, Al.AL_LOOPING, 1);
}
else
{
Al.alSourcei(channel, Al.AL_LOOPING, 0);
}
Al.alSourcePlay(channel);
Search WWH ::




Custom Search