15

How can I play a sound in WinForms with C#?

Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Moon
  • 19,518
  • 56
  • 138
  • 200

2 Answers2

25

For playing sound simply, with no interaction you can use System.Media.SoundPlayer:

System.Media.SoundPlayer player = new System.Media.SoundPlayer();
player.SoundLocation = "soundFile.wav";
player.Play();
Karl Johan
  • 4,012
  • 1
  • 25
  • 36
  • This doesn't work for me. I get "the format of the URI cannot be determined". Seems it needs a full path, not just a relative path. – Matt Sep 10 '20 at 22:17
9

NAudio is a great library to reproduce sound, you can find it here: http://naudio.codeplex.com/

And the tutorial is here: http://opensebj.blogspot.com/2009/02/introduction-to-using-naudio.html

Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
Sebastian Gray
  • 2,590
  • 3
  • 26
  • 36