1. Right click on your project name in solution explorer.
2. Point the cursor on Add then choose Existing Item...
3. Now go to the Location of your sound file and select that sound file.
4. Now select your sound file in solution explorer then Right click on it choose Properties and change its Build Action property(Content to Embedded Resource)
5.Build the program or one time Debug the program.
6. Now if you want to play sound file when a particular Form is loaded then use the given code in Form_Load event.
NOTE:-In this code Dreamer.wav is name of sound file.
ASHISH

Attachments Step_1_2.JPG 35.36 KB
using System.Reflection;
using System.IO;
using System.Resources;
using System.Media;
using System.Diagnostics;


namespace Yournamespace
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void Form2_Load(object sender, EventArgs e)
        {

            Assembly assembly;
            Stream soundStream;
            SoundPlayer sp;
            assembly = Assembly.GetExecutingAssembly();
            sp = new SoundPlayer(assembly.GetManifestResourceStream("Yournamespace.Dreamer.wav"));
            sp.Play(); 

        }

    }
}

It doesn't works ...

I have:

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

using System.Reflection;
using System.IO;
using System.Resources;
using System.Media;
using System.Diagnostics;

namespace Browser
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            Assembly assembly;
            Stream soundStream;
            SoundPlayer sp;
            assembly = Assembly.GetExecutingAssembly();
            sp = new SoundPlayer(assembly.GetManifestResourceStream("Browser.Billie_Jean.mp3"));
            sp.Play();
        }
        // etc.

When i debug my program , no sound sounds ... maybe because it's MP3 ?

Edited 6 Years Ago by charqus: n/a

Hi........
you are using different way to add sound file into Resources.
but in article i m using different way to add sound file into Resources.
plz use given step to sound file.
and also try different different sound file.
Actually, There are 2 - 3 way to add file into resources.

Tell me it is working or not?

Edited 6 Years Ago by ashishkumar008: n/a

Your SoundStream is declared, but not instantiated and in your code you do nothing with it.
Try:

Stream soundStream= new MemoryStream(Properties.Resources.MyWAVsong);
SoundPlayer player = new SoundPlayer(soundStream);
player.Play()

Edited 3 Years Ago by ddanbe

The sound of a lion is no different than any other sound you have in a WAV file, be it music the sound of traffic, breaking glass or whatever.
If you don't have a lion sound, record one in a zoo as an example and turn it into a WAV file, if it not already is.

Using System.Media;

// You do not need the filetype extension

SoundPlayer Sndplayr = new SoundPlayer(Properties.Resources.YourSoundFileName);
Sndplayr.Play();

Ahh Embedded Resources, I just learned about how to handle these at work when I had to work on someone else's source code.

There are multiple ways to handle these. You can add them as resources in the properties, at which point, you can use very short amount of codes to retrieve it, or you can add an existing item, and them choose the "Embedded Resource" option, at which point you have to extract the item and write it to file using a Byte Array.

I'll need to get the code from work, and I'll throw up both. I have nice examples of both (I use the Embedded Resources plus byte array part for about 13 .dlls + extra files and the Resources in the properties for a .exe that runs with my main program).

(If it's okay with the Original Poster, I'd like to make a new post about it ... I was actually going to last week, but ended up working through my lunch break).

Edited 2 Years Ago by JOSheaIV