Click here to Skip to main content
15,884,298 members
Articles / Programming Languages / C#

Add Sound File into Resource and Access It in .NET (C#)

Rate me:
Please Sign up or sign in to vote.
1.77/5 (11 votes)
20 Jan 2011CPOL1 min read 63.3K   812   11   11
How we can access sound file from resource, not from any other location

Introduction

Actually, in this article, I am accessing the sound file from our project resource. Many times, we use sound in our project but in earlier days, I placed the sound file in a different location and then I access it. It's useful for those who want to hide resource of project and they want to make the project portable.

Using the Code

  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 the name of the sound file.

Step_1_2.JPG

C#
//
// Any source code blocks look like this
//
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();  
        } 
    }
}

/*SoundPlayer sp = new SoundPlayer
(global::WindowsApplication1.Properties.Resources.yoursoundfilename); */

Points of Interest

Now, the time is 1:57 am, so I have to go. Actually, there are also some other techniques to access the sound file from resource. I just give one example for this which is mentioned in the above code in the comments.

History

  • 20th January, 2011: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student ORIENTAL INSTITUTE OF SCIENCE AND TECHNOLOGY, RGPV
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 1551045825-Jan-22 1:53
Member 1551045825-Jan-22 1:53 
GeneralMy vote of 1 Pin
Arlert4-Jan-15 1:10
Arlert4-Jan-15 1:10 
QuestionNeeded to create Resources folder with wav file Pin
Nozrul14-Nov-14 1:08
Nozrul14-Nov-14 1:08 
AnswerRe: Needed to create Resources folder with wav file Pin
ashishkumar00823-Nov-14 23:39
ashishkumar00823-Nov-14 23:39 
QuestionSound File into Resource Pin
famontepeque13-Mar-14 9:39
famontepeque13-Mar-14 9:39 
GeneralMy vote of 1 Pin
unpocoloco8-Sep-12 8:45
unpocoloco8-Sep-12 8:45 
GeneralMy vote of 5 Pin
Burak Tunçbilek19-Jun-12 4:50
Burak Tunçbilek19-Jun-12 4:50 
GeneralMy vote of 3 Pin
Baesky21-Jan-11 4:42
Baesky21-Jan-11 4:42 
GeneralMy vote of 3 Pin
Legol@s21-Jan-11 1:56
Legol@s21-Jan-11 1:56 
GeneralMy vote of 2 Pin
Volem20-Jan-11 22:25
Volem20-Jan-11 22:25 
GeneralC# Resources Pin
Ian Good20-Jan-11 11:37
Ian Good20-Jan-11 11:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.