I'm making a program in Visual Studio 2015 (C#) and I want to add sound effects to it. However, I have looked up countless tutorials but none of them seem to work, and gave me tons of errors. If anyone can give me a code to play a .wav file from resource files then I would be very grateful
Asked
Active
Viewed 2.2k times
-1
-
[use this](http://stackoverflow.com/a/6241374/4767498). you can also play multiple sounds together. – M.kazem Akhgary Aug 04 '15 at 16:29
-
WinForms? WPF?...something else? – Idle_Mind Aug 04 '15 at 16:33
-
Yes it says .wav in the question :) – Catquatwa Aug 04 '15 at 16:56
-
".wav" doesn't help. I was asking about the **type of application** you are writing; not the format of the sound file... – Idle_Mind Aug 04 '15 at 17:36
-
Sorry, that was a reply to a previous comment which has now been deleted – Catquatwa Aug 04 '15 at 18:16
4 Answers
0
If the file you want to play is wav files, try this.
var player = new System.Media.SoundPlayer("c:\\tes.wav");
player.Play();

Han
- 3,052
- 2
- 22
- 31
0
How to: Play Sounds in an Application
Add the following method code under the button1_Click event hander :
System.Media.SoundPlayer player =
new System.Media.SoundPlayer();
player.SoundLocation = @"C:\Users\Public\Music\Sample Music\xxxx.wav";
player.Load();
player.Play();

Tharif
- 13,794
- 9
- 55
- 77
-
if you set a proper path 100% output is assured..please go through the msdn reference..try to find the logic and dont run behind output alone :) – Tharif Aug 04 '15 at 16:29
-
-
0
For myself I wrote this SounceController, hope it help:
using System.Windows.Media; // add reference to system.windows.presentation.
using System;
using System.IO;
public class SoundController
{
private bool isPlaying;
private MediaPlayer player;
public SoundController()
{
player = new MediaPlayer();
}
~SoundController()
{
player = null;
}
public void Play(string path)
{
if (!File.Exists(path) || isPlaying)
return;
isPlaying = true;
player.Open(new Uri(path));
player.Play();
}
public void Stop()
{
if (isPlaying)
{
isPlaying = false;
player.Stop();
}
}
}

Artem Kulikov
- 2,250
- 19
- 32
-
Did you add a reference to System.Windows.Presentation? Also you need System and IO namespaces. I updated my answer. – Artem Kulikov Aug 04 '15 at 16:21
-
1@Catquatwa Please describe the errors you are having. It's going to be really difficult to offer an error free code without even seeing a sample of your code, or the problems you are encountering. – Matias Cicero Aug 04 '15 at 16:22
0
I recommend you use PInvoke To play sound using winmm.dll
first of all import System.Runtime.InteropServices
namespace in to your project.
using System.Runtime.InteropServices;
Then in your class you will have
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
public void Play(string path ,string name)
{
// Open
mciSendString($@"open {path} type waveaudio alias {name}", null, 0, IntPtr.Zero);
// Play
mciSendString($@"play {name}", null, 0, IntPtr.Zero);
}
You can play the sound sending correct path of wave file with name. . given name does not need to be the same name of wave file.for example:
Play(@"C:\soundeffect.wav", "soundEffect1");
Usually sound effects are played simultaneously. you can call this method several times to play several files simultaneously.
Play(@"C:\soundeffect1.wav", "soundEffect1");
Play(@"C:\soundeffect2.wav", "soundEffect2");
Play(@"C:\soundeffect3.wav", "soundEffect3");

M.kazem Akhgary
- 18,645
- 8
- 57
- 118