C#与SoundPlayer同时发出多个声音

时间:2015-03-17 13:46:30

标签: c# multithreading audio

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

我是否做过错误,或者我是否错过了解多线程这是可能的? 实现这个目标的正确方法是什么?

 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
}

1 个答案:

答案 0 :(得分:0)

SoundPlayer使用Windows MCI并且不是线程安全的。您可以使用其异步方法,但不能同时播放多个流,也不能同时播放声音。见这里:1

您需要使用另一个AudioLibrary,如DirectX或OpenAL。