8

I'm playing a game with some others coders. You get to write one oneline of C# and as many xaml lines as you want.

Anyone know how to play a sound in one line before the semi-colon;

This is as close as I can get so far

    SoundPlayer simpleSound = new SoundPlayer(@"c:\Media\pacman.wav");
    simpleSound.Play();

Edit: tried Blorgbeard's code, but sound isn't playing for some reason

if (Listbox.SelectedItem.ToString() == "2 Good 2 B True")
{
    try
    {
        (new SoundPlayer(@"/Project;component/sounds/pacman.wav")).Play();
    }
    catch (Exception ex)
    {
        Console.Error.WriteLine(ex.Message); 
    }

}
rd42
  • 3,584
  • 15
  • 56
  • 68

2 Answers2

16

No need for a variable:

(new SoundPlayer(@"c:\Media\pacman.wav")).Play();
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
8

Blorgbeard's code works for me if I use PlaySync() instead of Play():

(new SoundPlayer(soundFile)).PlaySync();

Play() plays sound in another thread. This may be the issue.

Yuriy
  • 388
  • 1
  • 3
  • 8