Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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#.

like image 644
Genius Avatar asked Nov 08 '10 16:11

Genius


People also ask

How do I listen to a WAV file?

Since WAV is quite a popular format, almost all devices today support it using built-in media players. On Windows, the Windows Media Player is capable of playing WAV files. On MacOS, iTunes or QuickTime can play WAV files. On Linux, your basic ALSA system can play these files.

What programs open WAV files?

WAV files are widely used, and because of this, many programs can open them on different platforms—Windows Media Player, Winamp, iTunes, VLC, and QuickTime, to name a few. Windows and macOS users can play WAV files right out of the box without having to install any third-party software.

What media player can play WAV files?

Poweramp Music Player is a powerful WAV file player for Android.


2 Answers

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

System.IO.Stream str = Properties.Resources.mySoundFile; System.Media.SoundPlayer snd = new System.Media.SoundPlayer(str); snd.Play(); 

SoundPlayer Class Documentation (MSDN)

like image 171
Evan Mulawski Avatar answered Sep 21 '22 07:09

Evan Mulawski


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.

open project resource

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

add audio files to resource

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

browsing for audio files

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

embedding audio files to resource

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

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. :)

like image 29
Pran Avatar answered Sep 18 '22 07:09

Pran