[英]C# Multiple sounds simultaneously with SoundPlayer
我想在程序中同時播放多個聲音。 看到我之前有很多人問了這個問題,我仔細檢查了他們,決定嘗試在不同線程上播放聲音。 但是,我仍然遇到相同的問題,即正在播放的聲音會被切成新的聲音。
我是否做錯了,還是我誤會了多線程的可能性? 實現該目標的正確方法是什么?
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
}
SoundPlayer使用Windows MCI,並且不是線程安全的。 您可以使用它們的異步方法,但是不能同時播放多個流,也不能同時播放聲音。 看這里: 1
您需要使用其他AudioLibrary,例如DirectX或OpenAL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.