C # play sound with a single line of C # code

I play a game with some other coders. You can write one oneline from C # and as many xaml lines as you want.

Does anyone know how to play sound on a single line before a semicolon;

It's as close as I can still

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

Edit: tried the Blorgbeard code, but the sound for some reason is not playing

 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); } } 
+8
c # audio
source share
2 answers

No need for variable:

 (new SoundPlayer(@"c:\Media\pacman.wav")).Play(); 
+16
source share

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

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

Play () plays sound in another stream. This can be a problem.

+8
source share

All Articles