简体   繁体   中英

C# Multiple sounds simultaneously with SoundPlayer

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
}

SoundPlayer uses Windows MCI and is not thread safe. You can use their async methods, but you can't play multiple streams at the same time or cannot be used to play sounds simultaneously. See here : 1

You need to use another AudioLibrary like DirectX or OpenAL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM