Sei sulla pagina 1di 7

c# - How to play WAV audio file from Resources? - Stack Overflow http://stackoverflow.

com/questions/4125698/how-to-play-wav-audio-file-from-resources

sign up log in tour help

x Dismiss

Join the Stack Overflow Community

Stack Overflow is a community of 6.4 million


programmers, just like you, helping each other.
Join them; it only takes a minute:

Sign up

How to play WAV audio file from Resources?

How can I play a WAV audio file in from my project's Resources? My project is a Windows Forms
application in C#.

c# visual-studio audio

edited May 31 '13 at 19:14 asked Nov 8 '10 at 16:18


Evan Mulawski Genius
38.5k 9 80 125 529 2 6 19

This is what you are looking for. Liviu M. Nov 8 '10 at 16:23

5 Answers

1 de 7 22-11-2016 22:12
c# - How to play WAV audio file from Resources? - Stack Overflow http://stackoverflow.com/questions/4125698/how-to-play-wav-audio-file-from-resources

Because mySoundFile is a Stream , you can take advantage of SoundPlayer 's overloaded
constructor, which accepts a Stream object:

Stream str = Properties.Resources.mySoundFile;


SoundPlayer snd = new SoundPlayer(str);
snd.Play();

SoundPlayer Class Documentation (MSDN)

edited May 31 '13 at 19:16 answered Nov 8 '10 at 16:24


Evan Mulawski
38.5k 9 80 125

This will throw an exception in Windows CE because it won't automatically convert the resource from a byte[]
to a stream. I found the following answer worked in that scenario. Leaving it here for others:
stackoverflow.com/questions/1900707/ Hagelt18 Dec 31 '14 at 18:21

We actually dont need to declare a separate Stream variable ;) TomeeNS Jun 10 at 0:00

@TomeeNS: Of course, but it shows people the type of the resource and the overload of the SoundPlayer
constructor that is used. Evan Mulawski Jun 13 at 13:51

a) OK, first add audio file (.wav) into project resource.

1. Open "Solution Explorer" from menu toolbar ("VIEW") or simply press Ctrl+Alt+L.
2. Click on drop-down list of "Properties".
3. Then select "Resource.resx" and press enter.

2 de 7 22-11-2016 22:12
c# - How to play WAV audio file from Resources? - Stack Overflow http://stackoverflow.com/questions/4125698/how-to-play-wav-audio-file-from-resources

4. Now select "Audio" from the combobox list.

5. Then click on "Add Resource", choose audio files (.wav) and click "Open".

3 de 7 22-11-2016 22:12
c# - How to play WAV audio file from Resources? - Stack Overflow http://stackoverflow.com/questions/4125698/how-to-play-wav-audio-file-from-resources

6. Select audio file(s) and change "Persistence" properties to "Embedded in .resx".

b) Now, just write this code to play the audio.

4 de 7 22-11-2016 22:12
c# - How to play WAV audio file from Resources? - Stack Overflow http://stackoverflow.com/questions/4125698/how-to-play-wav-audio-file-from-resources

In this code I'm playing audio on form load event.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using System.Media; // at first you've to import this package to access SoundPlayer

namespace WindowsFormsApplication1
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}

private void login_Load(object sender, EventArgs e)


{
playaudio(); // calling the function
}

private void playaudio() // defining the function


{
SoundPlayer audio = new
SoundPlayer(WindowsFormsApplication1.Properties.Resources.Connect); // here
WindowsFormsApplication1 is the namespace and Connect is the audio file name
audio.Play();
}
}
}

That's it.
All done, now run the project (press f5) and enjoy your sound.
All the best. :)

edited Jul 11 '15 at 11:22 answered Feb 17 '15 at 11:22


Pran
486 3 15

5 de 7 22-11-2016 22:12
c# - How to play WAV audio file from Resources? - Stack Overflow http://stackoverflow.com/questions/4125698/how-to-play-wav-audio-file-from-resources

Exactly what I was looking for! The pictures helped a lot. Do you know if I can embed a font (like
fontawesome) this way as well for use in my program? Xander Luciano Aug 2 at 19:52

Stream str = Properties.Resources.mySoundFile;


RecordPlayer rp = new RecordPlayer();
rp.Open(new WaveReader(str));
rp.Play();

from How to play WAV audio file from Resources in c#

edited Feb 2 '14 at 13:35 answered Feb 1 '14 at 22:08


Aleks
265 2 4

You need to be cautious about the garbage collector freeing up memory used by your sound
while the sound is still playing. While it rarely happens, when it does, you will just be playing
some random memory. There is a solution to this, complete with source code for achieving
what you want here: http://msdn.microsoft.com/en-us/library/dd743680(VS.85).aspx

Scroll to the very bottom, in the "Community Content" section.

answered Nov 8 '10 at 16:47


Brad
88.3k 22 158 278

When you have to add sounds into your project, you will do so by playing .wav file(s). Then
you have to add the .wav file(s) like this.

using System.Media; //write this at the top of the code

SoundPlayer my_wave_file = new SoundPlayer("F:/SOund wave file/airplanefly.wav");


my_wave_file.PlaySync(); // PlaySync means that once sound start then no other
activity if form will occur untill sound goes to finish

Remember that you have to write the path of the file with forward slashes ( / ) format, don't
use back slashes ( \ ) when giving a path to the file, else you will get an error.

Also note, if you want other things to happen while the sound is playing, you can change

6 de 7 22-11-2016 22:12
c# - How to play WAV audio file from Resources? - Stack Overflow http://stackoverflow.com/questions/4125698/how-to-play-wav-audio-file-from-resources

my_wave_file.PlaySync(); with my_wave_file.PlayAsync(); .

edited Jun 11 at 12:42 answered Mar 5 '14 at 15:41


David Wheatley Pir Fahim Shah
116 12 5,134 1 41 44

7 de 7 22-11-2016 22:12

Potrebbero piacerti anche