Playing sounds through c#
-
Hello helpers. So far, my asteroids game is coming along nicely, and I am learning lots. Now has come the time to get c# to play some sounds - by searching around I have found this: [DllImport("winmm.dll")] public static extern long PlaySound(String lpszName, long hModule, long dwFlags); This does is fact work, however, the keener amongst you will realise that code execution stops until this function returns a value. Of course, in my game, I don't want the game to freeze everytime I call this function, so, is there a) another method for playing a sound file (I used a com object in Foxpro) and if so, can somebody tell me how to do it in C# b) a way to call PlaySound without waiting for a return value? Many thanks you gorgeous hamsters :confused: J Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
-
Hello helpers. So far, my asteroids game is coming along nicely, and I am learning lots. Now has come the time to get c# to play some sounds - by searching around I have found this: [DllImport("winmm.dll")] public static extern long PlaySound(String lpszName, long hModule, long dwFlags); This does is fact work, however, the keener amongst you will realise that code execution stops until this function returns a value. Of course, in my game, I don't want the game to freeze everytime I call this function, so, is there a) another method for playing a sound file (I used a com object in Foxpro) and if so, can somebody tell me how to do it in C# b) a way to call PlaySound without waiting for a return value? Many thanks you gorgeous hamsters :confused: J Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
Use ASYNC flag that has a value 0x0001. You can use enum to include other flags: public enum PlaySoundFlag { // Play synchronously (default) Synchronously = 0x0000, // Play asynchronously Asynchronously = 0x0001, // Silence (!default) if sound not found NoDefault = 0x0002, // pszSound points to a memory file Memory = 0x0004, // Loop the sound until next sndPlaySound Loop = 0x0008, // Don't stop any currently playing sound NoStop = 0x0010, // Don't wait if the driver is busy NoWait = 0x00002000, // Name is a registry alias Alias = 0x00010000, // Alias is a predefined ID AliasId = 0x00110000, // Name is file name Filename = 0x00020000, // name is resource name or atom Resource = 0x00040004, // Purge non-static events for task Purge = 0x0040, // Look for application specific association Application = 0x0080 } Example: PlaySound("c:\\windows\\media\\Windows XP Startup.wav",0,PlaySoundFlag.Filename|PlaySoundFlag.Asynchronously); Should work Jerzy
-
Use ASYNC flag that has a value 0x0001. You can use enum to include other flags: public enum PlaySoundFlag { // Play synchronously (default) Synchronously = 0x0000, // Play asynchronously Asynchronously = 0x0001, // Silence (!default) if sound not found NoDefault = 0x0002, // pszSound points to a memory file Memory = 0x0004, // Loop the sound until next sndPlaySound Loop = 0x0008, // Don't stop any currently playing sound NoStop = 0x0010, // Don't wait if the driver is busy NoWait = 0x00002000, // Name is a registry alias Alias = 0x00010000, // Alias is a predefined ID AliasId = 0x00110000, // Name is file name Filename = 0x00020000, // name is resource name or atom Resource = 0x00040004, // Purge non-static events for task Purge = 0x0040, // Look for application specific association Application = 0x0080 } Example: PlaySound("c:\\windows\\media\\Windows XP Startup.wav",0,PlaySoundFlag.Filename|PlaySoundFlag.Asynchronously); Should work Jerzy
Or you could use an Asynchronous method call or make your synchronous call on a different thread. -- Peter Stephens
-
Use ASYNC flag that has a value 0x0001. You can use enum to include other flags: public enum PlaySoundFlag { // Play synchronously (default) Synchronously = 0x0000, // Play asynchronously Asynchronously = 0x0001, // Silence (!default) if sound not found NoDefault = 0x0002, // pszSound points to a memory file Memory = 0x0004, // Loop the sound until next sndPlaySound Loop = 0x0008, // Don't stop any currently playing sound NoStop = 0x0010, // Don't wait if the driver is busy NoWait = 0x00002000, // Name is a registry alias Alias = 0x00010000, // Alias is a predefined ID AliasId = 0x00110000, // Name is file name Filename = 0x00020000, // name is resource name or atom Resource = 0x00040004, // Purge non-static events for task Purge = 0x0040, // Look for application specific association Application = 0x0080 } Example: PlaySound("c:\\windows\\media\\Windows XP Startup.wav",0,PlaySoundFlag.Filename|PlaySoundFlag.Asynchronously); Should work Jerzy
Thanks alot, that's a whole lot of detail. The example clinches it. You are the man :cool:. Cheers J;) Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
-
Or you could use an Asynchronous method call or make your synchronous call on a different thread. -- Peter Stephens
Thanks for the advice. I will try to figure out how to make an asynchronous call - I assume this would be different to the flags that Jerzy is talking about? Thanks though, will look into it. Jase Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
-
Thanks for the advice. I will try to figure out how to make an asynchronous call - I assume this would be different to the flags that Jerzy is talking about? Thanks though, will look into it. Jase Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
Asynchronous calls in .NET use delegates. There are methods on a delegate called BeginInvoke and EndInvoke. I personally have not used async calls yet, but plan to do so soon. :) AFAIK .NET async calls use a threadpool behind the scenes. -- Peter Stephens
-
Use ASYNC flag that has a value 0x0001. You can use enum to include other flags: public enum PlaySoundFlag { // Play synchronously (default) Synchronously = 0x0000, // Play asynchronously Asynchronously = 0x0001, // Silence (!default) if sound not found NoDefault = 0x0002, // pszSound points to a memory file Memory = 0x0004, // Loop the sound until next sndPlaySound Loop = 0x0008, // Don't stop any currently playing sound NoStop = 0x0010, // Don't wait if the driver is busy NoWait = 0x00002000, // Name is a registry alias Alias = 0x00010000, // Alias is a predefined ID AliasId = 0x00110000, // Name is file name Filename = 0x00020000, // name is resource name or atom Resource = 0x00040004, // Purge non-static events for task Purge = 0x0040, // Look for application specific association Application = 0x0080 } Example: PlaySound("c:\\windows\\media\\Windows XP Startup.wav",0,PlaySoundFlag.Filename|PlaySoundFlag.Asynchronously); Should work Jerzy
Hey Jerzy, I tried this, but for some reason, none of the flags I pass in seem to make any difference. The sound does play though. Also, for some reason, when I use the enums as per your example, the code won't compile. I have tried subsituting the enums in the example for their values, but still things don't go according to my expectation (ie, makes no difference). Does this declaration look right for c# to you? public static extern long PlaySound(String lpszName, long hModule, long dwFlags); As far as I understand it, my alternatives are: 1. no sound at all 2. use a com object as per my original fox pro code 3. for every sound played, create a new thread, play the sound, kill the thread 4. do some asynch calls with beginInvoke and delegates and stuff. Don't want to do 1., 2. seems a good thing to learn in c#, in fact as does 3 and 4. 4 however I will have to leave until I have learnt a whole lot more or until someone explains the whole delegate thing to me. If you have any more suggestions along the lines of your original, or anything else for that matter, would love to hear 'em. Jase Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
-
Hey Jerzy, I tried this, but for some reason, none of the flags I pass in seem to make any difference. The sound does play though. Also, for some reason, when I use the enums as per your example, the code won't compile. I have tried subsituting the enums in the example for their values, but still things don't go according to my expectation (ie, makes no difference). Does this declaration look right for c# to you? public static extern long PlaySound(String lpszName, long hModule, long dwFlags); As far as I understand it, my alternatives are: 1. no sound at all 2. use a com object as per my original fox pro code 3. for every sound played, create a new thread, play the sound, kill the thread 4. do some asynch calls with beginInvoke and delegates and stuff. Don't want to do 1., 2. seems a good thing to learn in c#, in fact as does 3 and 4. 4 however I will have to leave until I have learnt a whole lot more or until someone explains the whole delegate thing to me. If you have any more suggestions along the lines of your original, or anything else for that matter, would love to hear 'em. Jase Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
I tested this code and it's working: .... .... .... using System.Runtime.InteropServices; public class Form1 : System.Windows.Forms.Form { public enum PlaySoundFlag { Synchronously = 0x0000, Asynchronously = 0x0001, NoDefault = 0x0002, Memory = 0x0004, Loop = 0x0008, NoStop = 0x0010, NoWait = 0x00002000, Alias = 0x00010000, AliasId = 0x00110000, Filename = 0x00020000, Resource = 0x00040004, Purge = 0x0040, Application = 0x0080 } [DllImport("winmm.dll")] public static extern int PlaySound(String pszSound, int hmod,PlaySoundFlag fdwSound); public Form1() { .... .... .... PlayTest(); .... .... .... } void PlayTest() { PlaySound("c:\\windows\\media\\Windows XP Startup.wav", 0, PlaySoundFlag.Filename|PlaySoundFlag.Asynchronously); } Jerzy
-
Hey Jerzy, I tried this, but for some reason, none of the flags I pass in seem to make any difference. The sound does play though. Also, for some reason, when I use the enums as per your example, the code won't compile. I have tried subsituting the enums in the example for their values, but still things don't go according to my expectation (ie, makes no difference). Does this declaration look right for c# to you? public static extern long PlaySound(String lpszName, long hModule, long dwFlags); As far as I understand it, my alternatives are: 1. no sound at all 2. use a com object as per my original fox pro code 3. for every sound played, create a new thread, play the sound, kill the thread 4. do some asynch calls with beginInvoke and delegates and stuff. Don't want to do 1., 2. seems a good thing to learn in c#, in fact as does 3 and 4. 4 however I will have to leave until I have learnt a whole lot more or until someone explains the whole delegate thing to me. If you have any more suggestions along the lines of your original, or anything else for that matter, would love to hear 'em. Jase Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk
Profox Jase wrote: public static extern long PlaySound(String lpszName, long hModule, long dwFlags); Your problem is the long type. In C# this is a 64 bit int. You should use int instead. -- Peter Stephens
-
Profox Jase wrote: public static extern long PlaySound(String lpszName, long hModule, long dwFlags); Your problem is the long type. In C# this is a 64 bit int. You should use int instead. -- Peter Stephens
Cool, thanks again. J Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk