1. Questions
2. Jobs
3. Documentation beta
4. Tags
5. Users
1.
2.
Log In Sign Up
x Dismiss
Join the Stack Overflow Community
Stack Overflow is a community of 6.9 million programmers, just like you, helping each other.
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
up vote -1
down vote c# audio visual-studio-2015 playback
favorite edited Aug 4 '15 at 16:32 asked Aug 4 '15 at 16:05
4 Answers
active oldest votes
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
up vote 0 Music\xxxx.wav";
down vote player.Load();
player.Play();
accepted
answered Aug 4 '15 at
16:11
tharif
7,58242551
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 4 '15 at 16:29
check edited code tharif Aug 4 '15 at 16:59
Finally Worked :) Thanks Catquatwa Aug 5 '15 at 11:15
Very happy to hear for your efforts that paid off :) tharif Aug 5 '15 at
11:16
add a comment
Handoko
1,3021819
add a comment
For myself I wrote this SounceController, hope it help:
public SoundController()
{
player = new MediaPlayer();
}
~SoundController()
{
player = null;
}
player.Open(new Uri(path));
player.Play();
}
public void Stop()
{
if (isPlaying)
{
isPlaying = false;
player.Stop();
}
}
}
answered Aug 4 '15 at 16:13
Artem Kulikov
2,0101133
Sorry, this has multiple errors for me :( Catquatwa Aug 4 '15 at 16:19
Did you add a reference to System.Windows.Presentation? Also you need
System and IO namespaces. I updated my answer. Artem Kulikov Aug 4 '15
at 16:21
@Catquatwa Please describe the errors you are having. It's going to be really
1 difficult to offer an error free code without even seeing a sample of your code,
or the problems you are encountering. Matias Cicero Aug 4 '15 at 16:22
add a comment
I recommend you use PInvoke To play sound using winmm.dll
using System.Runtime.InteropServices;
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, StringBuilder
buffer, int bufferSize, IntPtr hwndCallback);
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");
answered Aug 4 '15 at 16:26
M.kazem Akhgary
8,19741546
add a comment
Your Answer
Sign up or log in
Post as a guest
Name
Email
By posting your answer, you agree to the privacy policy and terms of service.
Remote
cc++
Remote
.nettdd
LaMotte CompanyChestertown, MD
$55k - $60kRemote
Remote
.nettdd
Linked
20
Play two sounds simultaneusly
Related
2
Visual Studio Play Sound With No File Present
7
Playing Audio in .Net / C#
2
Keep playing a sound over and over again in Matlab?
1
Play Background Sound and Sound Effect in Windows Phone 8
0
play sound on button click
-3
C# Play wav file with selected range time
201
What makes the Visual Studio debugger stop evaluating a ToString override?
1
error when playing a wav sound on cubieboard with qt using null output device, none
available
0
Playing a sound from an array returns an error C# - Visual Studio
2
Play entire sound file C# - Visual Studio
Counter-Fibonacci Sequences
Are these photos of protests posters against the Obama Administration real?
Escape a chessboard
What's actually wrong with an endpoint returning HTML rather than JSON data?
Superior Passtimes
question feed
about us tour help blog chat data legal privacy policy work here advertising info developer
jobs directory mobile contact us feedback
Culture /
Technology Life / Arts Science Other
Recreation
9. Bicy
cles
Engi
neeri
Impr 10. Role
ng
ove -
ment play
10. Unix
ing
&
8. Pers Gam
Linu ce
onal es
x
Fina
11. Expressi
nce 11. Ani
11. Ask onEngin
& me 10. more
Diffe e
Mon & (3)
rent Answers
ey Man
(App
ga
le) 12. Cryptogr
9. Aca
aphy
demi 12. Mot
12. Wor
a or
dPre
Vehi
ss
10. more cle
Deve
(8) Mai
lopm
nten
ent
ance
&
Rep
air
13. mor
e
(17)
site design / logo 2017 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0
with attribution required
rev 2017.3.16.25458