gpt4 book ai didi

c# - C#使用SoundPlayer同时播放多种声音

转载 作者:行者123 更新时间:2023-12-03 02:03:59 24 4
gpt4 key购买 nike

我想在程序中同时播放多个声音。看到很多在我之前的人问了这个问题,我仔细研究了他们,并决定尝试在不同线程上播放声音。但是,我仍然遇到同样的问题,即正在播放的声音被切成新的声音。

我是否做错了,还是我误会了多线程的可能性?
实现该目标的正确方法是什么?

 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。

关于c# - C#使用SoundPlayer同时播放多种声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29100724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com