954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

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

By Ashish Kumar on Feb 6th, 2010 3:24 pm

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

        }

    }
}

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
Junior Poster in Training
64 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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 !!!

charqus
Junior Poster in Training
64 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

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?

ashishkumar008
Light Poster
45 posts since Nov 2009
Reputation Points: 12
Solved Threads: 5
 

please tell me some other way for this problem.

Shlesh
Newbie Poster
6 posts since Nov 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You