Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Play WAV file from embedded resources.

Play WAV file from embedded resources.

Scheduled Pinned Locked Moved C#
questionhardwarelearning
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ranzask
    wrote on last edited by
    #1

    Hi all, I "add" a wav file to my project "file.wav" and made it embedded resource. now i have seen here and micosoft code to read and play it but its not working..what am i doing wrong? my code: private byte[] m_soundBytes; private enum Flags { SND_ASYNC = 0x0001, SND_MEMORY = 0x0004, } [DllImport("winmm.dll",CallingConvention = CallingConvention.Winapi)] static extern int PlaySound(byte[] wfname,int hMod,int sf); then there is the class WSounds: public WSounds(string s) { string sss = Assembly.GetExecutingAssembly().GetName().ToString(); Stream a = Assembly.GetExecutingAssembly().GetManifestResourceStream(sss + "." + s); m_soundBytes = new byte[a.Length]; a.Read(m_soundBytes, 0, (int)a.Length); } public void Play() { PlaySound(m_soundBytes, 0, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY)); } this line throws exception : m_soundBytes = new byte[a.Length]; so i guess something with the resource is wrong.. i tried another code too: System.IO.Stream strTheme = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("file.wav"); byte[] bytTheme = new byte[strTheme.Length]; strTheme.Read(bytTheme, 0, (int) strTheme.Length); by the way: how do i need to add a WAV file ? under resources sub-dir or on the main project? Thanks in advance, Ran R.Z

    M S 2 Replies Last reply
    0
    • R ranzask

      Hi all, I "add" a wav file to my project "file.wav" and made it embedded resource. now i have seen here and micosoft code to read and play it but its not working..what am i doing wrong? my code: private byte[] m_soundBytes; private enum Flags { SND_ASYNC = 0x0001, SND_MEMORY = 0x0004, } [DllImport("winmm.dll",CallingConvention = CallingConvention.Winapi)] static extern int PlaySound(byte[] wfname,int hMod,int sf); then there is the class WSounds: public WSounds(string s) { string sss = Assembly.GetExecutingAssembly().GetName().ToString(); Stream a = Assembly.GetExecutingAssembly().GetManifestResourceStream(sss + "." + s); m_soundBytes = new byte[a.Length]; a.Read(m_soundBytes, 0, (int)a.Length); } public void Play() { PlaySound(m_soundBytes, 0, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY)); } this line throws exception : m_soundBytes = new byte[a.Length]; so i guess something with the resource is wrong.. i tried another code too: System.IO.Stream strTheme = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("file.wav"); byte[] bytTheme = new byte[strTheme.Length]; strTheme.Read(bytTheme, 0, (int) strTheme.Length); by the way: how do i need to add a WAV file ? under resources sub-dir or on the main project? Thanks in advance, Ran R.Z

      M Offline
      M Offline
      Marc 0
      wrote on last edited by
      #2

      I had troubles with exactly this stuff last week.

      ranzask wrote:

      this line throws exception : m_soundBytes = new byte[a.Length]; so i guess something with the resource is wrong..

      The Stream a is probably null, because the assembly couldn't fine the resource. "file.wav" won't work either, because you have to prefix the file.wav with the project namespace (at least in VB.Net). You can do

      string[] names = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
      foreach (string name in names){
      Debug.WriteLine(name)
      }

      and look for the name you want (this is the way i did it :)).

      ranzask wrote:

      by the way: how do i need to add a WAV file ? under resources sub-dir or on the main project?

      That doesn't matter. Whatever the folder may be, it doesn't matter for the compiler, just like C# source files. HTH! :)


      "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick


      || Fold With Us! || Pensieve || VG.Net ||

      R C 2 Replies Last reply
      0
      • R ranzask

        Hi all, I "add" a wav file to my project "file.wav" and made it embedded resource. now i have seen here and micosoft code to read and play it but its not working..what am i doing wrong? my code: private byte[] m_soundBytes; private enum Flags { SND_ASYNC = 0x0001, SND_MEMORY = 0x0004, } [DllImport("winmm.dll",CallingConvention = CallingConvention.Winapi)] static extern int PlaySound(byte[] wfname,int hMod,int sf); then there is the class WSounds: public WSounds(string s) { string sss = Assembly.GetExecutingAssembly().GetName().ToString(); Stream a = Assembly.GetExecutingAssembly().GetManifestResourceStream(sss + "." + s); m_soundBytes = new byte[a.Length]; a.Read(m_soundBytes, 0, (int)a.Length); } public void Play() { PlaySound(m_soundBytes, 0, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY)); } this line throws exception : m_soundBytes = new byte[a.Length]; so i guess something with the resource is wrong.. i tried another code too: System.IO.Stream strTheme = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("file.wav"); byte[] bytTheme = new byte[strTheme.Length]; strTheme.Read(bytTheme, 0, (int) strTheme.Length); by the way: how do i need to add a WAV file ? under resources sub-dir or on the main project? Thanks in advance, Ran R.Z

        S Offline
        S Offline
        Sean Michael Murphy
        wrote on last edited by
        #3

        ranzask wrote:

        I "add" a wav file to my project "file.wav" and made it embedded resource. now i have seen here and micosoft code to read and play it but its not working..what am i doing wrong?

        You could check out my article here[^] Share and enjoy. Sean

        R 1 Reply Last reply
        0
        • S Sean Michael Murphy

          ranzask wrote:

          I "add" a wav file to my project "file.wav" and made it embedded resource. now i have seen here and micosoft code to read and play it but its not working..what am i doing wrong?

          You could check out my article here[^] Share and enjoy. Sean

          R Offline
          R Offline
          ranzask
          wrote on last edited by
          #4

          i allready saw that , its too complicated for me :-) but i figured out my mistake. R.Z

          S 1 Reply Last reply
          0
          • M Marc 0

            I had troubles with exactly this stuff last week.

            ranzask wrote:

            this line throws exception : m_soundBytes = new byte[a.Length]; so i guess something with the resource is wrong..

            The Stream a is probably null, because the assembly couldn't fine the resource. "file.wav" won't work either, because you have to prefix the file.wav with the project namespace (at least in VB.Net). You can do

            string[] names = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
            foreach (string name in names){
            Debug.WriteLine(name)
            }

            and look for the name you want (this is the way i did it :)).

            ranzask wrote:

            by the way: how do i need to add a WAV file ? under resources sub-dir or on the main project?

            That doesn't matter. Whatever the folder may be, it doesn't matter for the compiler, just like C# source files. HTH! :)


            "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick


            || Fold With Us! || Pensieve || VG.Net ||

            R Offline
            R Offline
            ranzask
            wrote on last edited by
            #5

            Thanks alot !! i did it and change my resource name to include the namespace too and it worked :-) Thanks again. Ran.:laugh: R.Z

            1 Reply Last reply
            0
            • M Marc 0

              I had troubles with exactly this stuff last week.

              ranzask wrote:

              this line throws exception : m_soundBytes = new byte[a.Length]; so i guess something with the resource is wrong..

              The Stream a is probably null, because the assembly couldn't fine the resource. "file.wav" won't work either, because you have to prefix the file.wav with the project namespace (at least in VB.Net). You can do

              string[] names = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
              foreach (string name in names){
              Debug.WriteLine(name)
              }

              and look for the name you want (this is the way i did it :)).

              ranzask wrote:

              by the way: how do i need to add a WAV file ? under resources sub-dir or on the main project?

              That doesn't matter. Whatever the folder may be, it doesn't matter for the compiler, just like C# source files. HTH! :)


              "..Commit yourself to quality from day one..it's better to do nothing at all than to do something badly.." -- Mark McCormick


              || Fold With Us! || Pensieve || VG.Net ||

              C Offline
              C Offline
              Curtis Schlak
              wrote on last edited by
              #6

              [Marc] wrote:

              because you have to prefix the file.wav with the project namespace (at least in VB.Net).

              You have to do it in C#, too. <smile /> "we must lose precision to make significant statements about complex systems." -deKorvin on uncertainty

              1 Reply Last reply
              0
              • R ranzask

                i allready saw that , its too complicated for me :-) but i figured out my mistake. R.Z

                S Offline
                S Offline
                Saravanan_article
                wrote on last edited by
                #7

                Hi, Its very complicated to see the code,, so please post ur sample piece of code, and also post what is the problem u are facing and whts the solun you found.. its useful for all the beginners as well as developers.. Bye

                R 1 Reply Last reply
                0
                • S Saravanan_article

                  Hi, Its very complicated to see the code,, so please post ur sample piece of code, and also post what is the problem u are facing and whts the solun you found.. its useful for all the beginners as well as developers.. Bye

                  R Offline
                  R Offline
                  ranzask
                  wrote on last edited by
                  #8

                  the final code is: using System; using System.Runtime.InteropServices; using System.Diagnostics; using System.Threading; using System.IO; using System.Reflection; namespace yournamespace { public class WSounds { private byte[] m_soundBytes; private bool disable=false; private enum Flags { SND_ASYNC = 0x0001, SND_MEMORY = 0x0004 } [DllImport("winmm.dll",CallingConvention = CallingConvention.Winapi)] static extern int PlaySound(byte[] wfname,int hMod,int sf); public WSounds(string s) { if (s == null) { disable = true; return; } try { Stream a = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(s); m_soundBytes = new byte[a.Length]; a.Read(m_soundBytes, 0, (int)a.Length); } catch (Exception e) { disable = true; } } public void Play() { if (!disable) PlaySound(m_soundBytes, 0, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY)); } } } then in your main code you must add a file to your resources list "file.wav" for example that will be along the classes..and change his property to :"embedded resources" so it will compile him along with the exe. and you call the class this way: private WSounds ws; ws = new WSounds("yournamespace.file.wav"); ws.Play(); thats it. R.Z -- modified at 6:05 Monday 30th January, 2006

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups