How to play WAV audio file from Resources?

91,720

Solution 1

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

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);
snd.Play();

SoundPlayer Class Documentation (MSDN)

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

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.

Solution 5

Theses two lines can do it:

SoundPlayer sound = new SoundPlayer(Properties.Resources.solo);     
sound.Play(); 
Share:
91,720
Genius
Author by

Genius

Updated on July 09, 2022

Comments

  • Genius
    Genius 6 months

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

  • Hagelt18
    Hagelt18 almost 8 years
    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/…
  • TomeeNS
    TomeeNS over 6 years
    We actually dont need to declare a separate Stream variable ;)
  • Evan Mulawski
    Evan Mulawski over 6 years
    @TomeeNS: Of course, but it shows people the type of the resource and the overload of the SoundPlayer constructor that is used.
  • Xander Luciano
    Xander Luciano over 6 years
    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?
  • Matt
    Matt about 5 years
    When I add a wav file it adds a Resources2.Designer.cs which seems to be a duplicate of Resources.Designer.cs - thus giving me conflicts. This just started happening.Any ideas what could be happening? screencast.com/t/xAVpE5v6b0
  • René W.
    René W. over 4 years
    This Answer is better than the marked answer cause you don't need extra classes that might be outdated. As well the Writer did type the Namespaces as well.