[Solved] 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#.

Enquirer: Genius


Solution #1:

  Stream str = Properties.Resources.mySoundFile;
  RecordPlayer rp = new RecordPlayer();
  rp.Open(new WaveReader(str));

From How to play WAV audio file from resources in C#.

Respondent: Aleks

Solution #2:

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);

SoundPlayer Class Documentation (MSDN)

Respondent: Evan Mulawski

Solution #3:

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()

        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

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

Respondent: Pran

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

Respondent: Brad

Solution #5:

Theses two lines can do it:

SoundPlayer sound = new SoundPlayer(Properties.Resources.solo);     

Solution #6:

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 my_wave_file.PlaySync(); with my_wave_file.PlayAsync();.

Respondent: Pir Fahim Shah

Solution #7:

There are two ways to do so as far as I know, list below:

  1. Use file path

First put the file in the root folder of the project, then no matter you run the program under Debug or Release mode, the file can both be accessed for sure. Then use the class SoundPlayer to paly it.
But in this way, if you want to release the project to users, you need to copy the sound files with its folder hierarchies except hierarchies the folder “Release” under the “bin” directory.

        var basePath = System.AppDomain.CurrentDomain.BaseDirectory;
        SoundPlayer player = new SoundPlayer();
        player.SoundLocation = Path.Combine(basePath, @"./../../Reminder.wav");
  1. Use resource

Follow below animate, add “Exsiting file” to the project.

enter image description here

        SoundPlayer player = new SoundPlayer(Properties.Resources.Reminder);

The strength of this way is:
Only the folder “Release” under the “bin” directory need to be copy when run the program.

Respondent: Bravo Yeung

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Leave a Reply

Your email address will not be published.