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. Playing sounds through c#

Playing sounds through c#

Scheduled Pinned Locked Moved C#
learningcsharpcomgame-devalgorithms
10 Posts 3 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.
  • P Offline
    P Offline
    Profox Jase
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • P Profox Jase

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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

      P P 3 Replies Last reply
      0
      • L Lost User

        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

        P Offline
        P Offline
        Peter Stephens
        wrote on last edited by
        #3

        Or you could use an Asynchronous method call or make your synchronous call on a different thread. -- Peter Stephens

        P 1 Reply Last reply
        0
        • L Lost User

          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

          P Offline
          P Offline
          Profox Jase
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • P Peter Stephens

            Or you could use an Asynchronous method call or make your synchronous call on a different thread. -- Peter Stephens

            P Offline
            P Offline
            Profox Jase
            wrote on last edited by
            #5

            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

            P 1 Reply Last reply
            0
            • P Profox Jase

              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

              P Offline
              P Offline
              Peter Stephens
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • L Lost User

                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

                P Offline
                P Offline
                Profox Jase
                wrote on last edited by
                #7

                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

                L P 2 Replies Last reply
                0
                • P Profox Jase

                  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

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • P Profox Jase

                    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

                    P Offline
                    P Offline
                    Peter Stephens
                    wrote on last edited by
                    #9

                    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

                    P 1 Reply Last reply
                    0
                    • P 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

                      P Offline
                      P Offline
                      Profox Jase
                      wrote on last edited by
                      #10

                      Cool, thanks again. J Jason King jason.king@profox.co.uk Feel the love at www.profox.co.uk

                      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