Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# play sound with one line of c# code

Tags:

c#

audio

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); 
    }

}
like image 235
rd42 Avatar asked Apr 22 '11 15:04

rd42


2 Answers

No need for a variable:

(new SoundPlayer(@"c:\Media\pacman.wav")).Play();
like image 128
Blorgbeard Avatar answered Sep 21 '22 13:09

Blorgbeard


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.

like image 39
Yuriy Avatar answered Sep 21 '22 13:09

Yuriy