Click here to Skip to main content
Click here to Skip to main content

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

, 20 Jan 2011
Rate this:
Please Sign up or sign in to vote.
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

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

Share

About the Author

ashishkumar008
Student ORIENTAL INSTITUTE OF SCIENCE AND TECHNOLOGY, RGPV
India India
No Biography provided

Comments and Discussions

 
QuestionSound File into Resource Pinmemberfamontepeque13-Mar-14 9:39 
GeneralMy vote of 1 Pinmemberunpocoloco8-Sep-12 8:45 
GeneralMy vote of 5 PinmemberBurak Tunçbilek19-Jun-12 4:50 
GeneralMy vote of 3 PinmemberBaesky21-Jan-11 4:42 
GeneralMy vote of 3 PinmemberLegol@s21-Jan-11 1:56 
GeneralMy vote of 2 PinmemberVolem20-Jan-11 22:25 
GeneralC# Resources PinmemberIGood20-Jan-11 11:37 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 20 Jan 2011
Article Copyright 2011 by ashishkumar008
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid