Play WAV file from embedded resources.
-
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 -
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.ZI 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 dostring[] 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 ||
-
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 -
-
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 dostring[] 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 ||
-
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 dostring[] 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 ||
[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
-
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
-
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
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