I want to play multiple sounds at the same time in my program. Seeing that a lot of people before me have asked this question I looked through them, and decided to try to play the sounds on different threads. However I am still having the same issue where a new sound cuts of the one currently playing.
Have I done a misstake, or did I missunderstand that multithreading this would be possible? What would be the proper way of achieveing this?
public class SoundManager
{
private const int NUM_EFFECT_CHANNELS = 8;
private const int NUM_AMBIENT_CHANNELS = 2;
private static Thread[] ambientSounds = new Thread[NUM_AMBIENT_CHANNELS];
private static Thread[] soundEffects = new Thread[NUM_EFFECT_CHANNELS];
private static bool[] isPlaying = new bool[NUM_EFFECT_CHANNELS];
private static bool[] isAmbientPlaying = new bool[NUM_AMBIENT_CHANNELS];
#region public
public static void PlayAmbientSound(string soundFileName)
{
for (int channel = 0; channel < NUM_AMBIENT_CHANNELS; channel++)
{
if (isAmbientPlaying[channel])
continue;
ambientSounds[channel] = new Thread(() => { ambientPlayer(soundFileName, false, channel); });
isAmbientPlaying[channel] = true;
ambientSounds[channel].Start();
return;
}
}
public static void PlayAmbientSound(string soundFileName, bool loop)
{
for (int channel = 0; channel < NUM_AMBIENT_CHANNELS; channel++)
{
if (isAmbientPlaying[channel])
continue;
ambientSounds[channel] = new Thread(() => { ambientPlayer(soundFileName, loop, channel); });
isAmbientPlaying[channel] = true;
ambientSounds[channel].Start();
return;
}
}
public static void PlaySoundEffect(string soundFileName)
{
for (int channel = 0; channel < NUM_EFFECT_CHANNELS; channel++)
{
if (isPlaying[channel])
continue;
soundEffects[channel] = new Thread(() => { effectPlayer(soundFileName, false, channel); });
isPlaying[channel] = true;
soundEffects[channel].Start();
return;
}
}
public static void PlaySoundEffect(string soundFileName, bool loop)
{
for (int channel = 0; channel < NUM_EFFECT_CHANNELS; channel++)
{
if (isPlaying[channel])
continue;
soundEffects[channel] = new Thread(() => { effectPlayer(soundFileName, loop, channel); });
isPlaying[channel] = true;
soundEffects[channel].Start();
return;
}
}
public static void StopAmbient(int channel)
{
ambientSounds[channel].Abort();
isAmbientPlaying[channel] = false;
}
public static void StopEffect(int channel)
{
soundEffects[channel].Abort();
isPlaying[channel] = false;
}
#endregion
#region private
private static void effectPlayer(string soundFileName, bool loop, int channel)
{
Console.WriteLine("Started Effect on channel: " + channel + "...");
if (loop)
new SoundPlayer(soundFileName).PlayLooping();
else
new SoundPlayer(soundFileName).Play();
isPlaying[channel] = false;
Console.WriteLine("Channel " + channel + " finnished");
}
private static void ambientPlayer(string soundFileName, bool loop, int channel)
{
Console.WriteLine("Started Ambient on channel: " + channel + "...");
if (loop)
new SoundPlayer(soundFileName).PlayLooping();
else
new SoundPlayer(soundFileName).Play();
isAmbientPlaying[channel] = false;
Console.WriteLine("Channel " + channel + " finnished");
}
#endregion
}