Play Sound in .Net

Snippets below have been condensed from their original sources for brevity. See references for original articles.

All examples are in C# .Net.


        public static enum SoundSource {
        FromFileSystem = 0,

        public static void PlaySound(string Filename, SoundSource Source) {
            switch (Source) {
                case SoundSource.FromFileSystem:
                    SoundPlayer objPlayer = new SoundPlayer();
                    objPlayer.SoundLocation="C:\\" + Filename;
                case SoundSource.FromResourceStream:
                    SoundPlayer objPlayer2 = new SoundPlayer();
                    objPlayer2.Stream = GetEmbeddedResourceStream(Filename);

        private static System.IO.Stream GetEmbeddedResourceStream(string FileName) {
            Assembly objAssembly;
            System.IO.Stream soundstream;
            objAssembly = Assembly.LoadFrom();
            soundstream = objAssembly.GetManifestResourceStream(FileName);
            return soundstream;

ASP .Net: (create an empty div in your page called “sound_element”)

        public static void PlaySound(Page PageInstance)
            ClientScriptManager ClientScript = PageInstance.ClientScript;

            string soundclipscript =
                "<script type=\"text/javascript\">" +
    "$('#sound_element').html(\"<embed src='\"+ PageInstance.ResolveUrl(\"~\") + "Sounds/ambient1.mp3" +\"' hidden=true autostart=true loop=false>\");" +
            AjaxControlToolkit.ToolkitScriptManager.RegisterStartupScript(PageInstance, PageInstance.GetType(), "playsound",
                soundclipscript, false);


Note there are many ways to play audio in a page, and the above snippet is not necessarily the best approach for every browser.

New HTML5 audio tag:

<audio src="horse.ogg" controls="controls">
Your browser does not support the audio element.

W3C Schools,


About Ronnie Diaz

Ronnie Diaz is a software engineer and tech consultant. Ronnie started his career in front-end and back-end development for companies in ecommerce, service industries and remote education. This work transitioned from traditional desktop client-server applications through early cloud development. Software included human resource management and service technician workflows, online retail e-commerce and electronic ordering and fulfillment, IVR customer relational systems, and video streaming remote learning SCORM web applications. Hands on server experience and software performance optimization led to creation of a startup business focused on collocated data center services and continued experience with video streaming hardware and software. This led to a career in Amazon Prime Video where Ronnie is currently employed, building software and systems which stream live sports and events for millions of viewers around the world.

Posted on February 4, 2011, in Programming & Development and tagged , , , , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: