1

I've been trying to have two sounds playing at the same time (one which is looping and one that starts on keypress) but one sound will cancel the other out and when I try to play them again I get errors (ThreadStateException was unhandled)

There was a similar question asked on stack overflow but there wasn't a really a valid answer

public partial class Form1 : Form
    {
        Thread BackgroundSoundThread = new Thread(BST);
        Thread LaserSoundThread = new Thread(LST);
    }
static void BST()
    {
        System.Media.SoundPlayer sPlayer = new System.Media.SoundPlayer();
        sPlayer.SoundLocation = "Game Ambient Forest loop.wav";
        sPlayer.PlayLooping();
    }
static void LST()
    {
        System.Media.SoundPlayer LaserSound = new System.Media.SoundPlayer();
        LaserSound.SoundLocation = "Single Laser.wav";
        LaserSound.Play();
    }

private void Form1_Load(object sender, EventArgs e)
    {
        BackgroundSoundThread.Start();
    }
private void Form1_KeyDown(object sender, KeyEventArgs e)
    {
       LaserSoundThread.Start();
    }

Can someone help me understand what's going on and offer a possible solution?

Andrew Neate
  • 109
  • 1
  • 9
  • 2
    i dont think `SoundPlayer` is designed for that. – Daniel A. White Apr 20 '17 at 11:57
  • 1. What errors - post the exact messages (and on which line of code they occur) 2. Sound cards/devices are usually exclusive resources - most of them cannot play two separate sounds at once - you have to mix them yourself (e.g.: using a library/framework that supports it) – UnholySheep Apr 20 '17 at 11:58
  • 2
    Have a look at `NAudio`. Can do a lot of cool stuff. Start [here](http://mark-dot-net.blogspot.be/2014/02/fire-and-forget-audio-playback-with.html) and search around on the web. – Wim Ombelets Apr 20 '17 at 12:00
  • guessing it does not like calling start on the same thread each keypress either – WraithNath Apr 20 '17 at 15:56

0 Answers0