Anda di halaman 1dari 10

Stack Overflow

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.

Join them; it only takes a minute:


Sign up

Play Sounds in a Visual Studio Application


Ask Question

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

shareimprove this question

Artem Kulikov Catquatwa


2,0101133 5711412
use this. you can also play multiple sounds together. M.kazem Akhgary
Aug 4 '15 at 16:29
WinForms? WPF?...something else? Idle_Mind Aug 4 '15 at 16:33
Yes it says .wav in the question :) Catquatwa Aug 4 '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 4 '15 at 17:36
Sorry, that was a reply to a previous comment which has now been deleted
Catquatwa Aug 4 '15 at 18:16
add a comment

4 Answers
active oldest votes

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
up vote 0 Music\xxxx.wav";
down vote player.Load();
player.Play();
accepted
answered Aug 4 '15 at
16:11

shareimprove this edited Aug 4 '15 at


answer 16:59

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

If the file you want to play is wav files, try this.

var player = new System.Media.SoundPlayer("c:\\tes.wav");


player.Play();
up vote 0 answered Aug 4 '15 at 16:08
down vote

shareimprove this answer

Handoko
1,3021819
add a comment
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)


up vote 0 {
down if (!File.Exists(path) || isPlaying)
return;
vote
isPlaying = true;

player.Open(new Uri(path));
player.Play();
}
public void Stop()
{
if (isPlaying)
{
isPlaying = false;
player.Stop();
}
}
}
answered Aug 4 '15 at 16:13

shareimprove this answer edited Aug 4 '15 at 16:21

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

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);
up vote 0 // Play
mciSendString($@"play {name}", null, 0, IntPtr.Zero);
down }
vote
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");
answered Aug 4 '15 at 16:26

shareimprove this answer edited Aug 4 '15 at 17:05

M.kazem Akhgary
8,19741546
add a comment

Your Answer

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password

Post as a guest

Name
Email

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions


tagged c# audio visual-studio-2015 playback or ask your
own question.

asked 1 year ago

viewed 3153 times

active 1 year ago

Work from anywhere

Lisp Software Architect - $60k

CrossoverNo office location

Remote

cc++

C# (.NET) Software Architect - $60k

CrossoverNo office location

Remote

.nettdd

Back End Developer

LaMotte CompanyChestertown, MD

$55k - $60kRemote

C# (.NET) Chief Software Architect - $100k

CrossoverNo office location

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

Hot Network Questions

How do I throw a boomerang or spear in Breath of the Wild?

My boss' new hire, a friend of his, is making advances on me

Counter-Fibonacci Sequences

Are these photos of protests posters against the Obama Administration real?

How to deal with an inappropriate greeting in an email?

How do I judge when and how much time passes in game?

Escape a chessboard

What's actually wrong with an endpoint returning HTML rather than JSON data?

Shortest path connecting two opposite points on a cube

Does the International Math Olympiad help research mathematics?

Is it legal to sell scale models of buildings I have no affiliation with?

What is going on with Drupal Packagist?

A question in Real Analysis

Two figures with different aspect ratio

"Do never..." vs. "Do not ever..."

What's the difference between GMT+01:00 Amsterdam and GMT+01:00 Brussels?

Who wins the House Cup in books 4, 5, 6, and 7?

Dealing with the unexpected death of an employee

Why did Voldemort kill Lily Potter instead of stunning her?

Representation theory and elementary particles

Superior Passtimes

This is the sort of challenge that bytes


Growing a shape with changing color

Is it safe to drive I/O pins of an unpowered AVR ATmega328P?

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

1. Stac 1. Geograp 1. Code 1. Phot 1. Engl 1. Math 1. M


k hic Revi ogra ish Overf eta
Over Informat ew phy Lan low Sta
flow ion guag ck
Systems 2. Mag 2. Scie e& 2. Math Ex
2. Serv ento nce Usa emati ch
er 2. Electrica Ficti ge cs an
Fault l 3. Sign on & ge
Engineer al Fant 2. Ske 3. Cross
3. Supe ing Proc asy ptics Valid 2. Sta
r essin ated ck
User 3. Android g 3. Grap 3. Mi (stats Ap
Enthusia hic Yod ) ps
4. Web sts 4. Rasp Desi eya
Appl berry gn (Jud 4. Theor 3. Ar
icati 4. Informat Pi aism etical ea
ons ion 4. Mov ) Com 51
Security 5. Prog ies puter
5. Ask ram & 4. Trav Scien 4. Sta
Ubu 5. Databas ming TV el ce ck
ntu e Puzz Ov
Adminis les & 5. Musi 5. Chri 5. Physi erf
6. Web trators Code c: stian cs lo
mast Golf Pract ity w
ers 6. Drupal ice 6. Chem Tal
Answers 6. more & 6. Engl istry ent
7. Gam (7) Theo ish
e 7. SharePoi ry Lan 7. Biolo
Deve nt guag gy
lopm 6. Seas e
ent 8. User oned Lear 8. Com
Experien Advi ners puter
8. TeX ce ce Scien
- (coo 7. Japa ce
LaTe 9. Mathem king nese
X atica ) Lan 9. Philo
guag sophy
9. Soft 10. Salesfor 7. Hom e
ware e
8. Arq
ade
(ga
min
g)

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

Anda mungkin juga menyukai