Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
com/questions/4125698/how-to-play-wav-audio-file-from-resources
x Dismiss
Sign up
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
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:
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
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
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
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
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;
namespace WindowsFormsApplication1
{
public partial class login : Form
{
public login()
{
InitializeComponent();
}
That's it.
All done, now run the project (press f5) and enjoy your sound.
All the best. :)
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
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
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.
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
7 de 7 22-11-2016 22:12