Add sound file into Resource and access it. in .net (C#)

ashishkumar008 3 Tallied Votes 4K Views Share

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

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

        }

    }
}
charqus 0 Junior Poster in Training

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 ?

charqus 0 Junior Poster in Training

I can't edit my post
--------------------------
Look at this: http://files.uploadffs.com/d/a/f2b0a5b2/code.PNG
I press F5 , and my sound doesn't sing !!!

ashishkumar008 2 Light Poster

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?

Shlesh 0 Newbie Poster

please tell me some other way for this problem.

ddanbe 2,724 Professional Procrastinator Featured Poster

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()
shahbaz aziz 0 Newbie Poster

i want to add a sound of lion in our project...so how can i add tis.....please give the coding

ddanbe 2,724 Professional Procrastinator Featured Poster

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.

pradeep_8 0 Newbie Poster

i need to read .mp3 from list box how can read it i mean play it

pradeep_8 0 Newbie Poster

i listed my files in list i need to play it using button how can i read it

Sharp_1 0 Newbie Poster
Using System.Media;

// You do not need the filetype extension

SoundPlayer Sndplayr = new SoundPlayer(Properties.Resources.YourSoundFileName);
Sndplayr.Play();
JOSheaIV 119 C# Addict

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.