www.xbdev.net
xbdev - software development
Saturday February 22, 2025
Home | Contact | Support | Programming.. With C# and .Net...
     
 

Programming..

With C# and .Net...

 


Sound - Playing you .wav files.

 

 

Well you need some sound...even if its just a warning message.  But playing a nice .wav in your background, how would we do that?  Don't you go worrying yourself, we'll do that here...

 

DownloadSourceCode

 

Simple examples are always good...

 

 

code: DownloadSourceCode

// sound.cs - version-1-

 

// C:>csc /t:winexe sound.cs

 

// Well we want our program to be in a GUI window, so we need to inherit

// from Form, which is in the library 'System.Windows.Forms'.

 

using System;                           // Its for 'string' variable type

using System.Windows.Forms;             // use it for 'Application.StarupPath'

using System.Runtime.InteropServices;   // so we can use a win32 dll

 

 

class SimpleWindow : System.Windows.Forms.Form

{

      [DllImport("winmm.dll")]

      public static extern long PlaySound(String lpszName, long hModule, long dwFlags);

 

      // Window constructor

      SimpleWindow()

      {

            string wavefile = "indianajones.wav";

            PlaySound(Application.StartupPath + "\\" + wavefile, 0, 0);

      }

 

      // Our program entry point.

      static void Main()

      {

            System.Windows.Forms.Application.Run(new SimpleWindow());

      }

}// End of SimpleWindow Class

 

 

Well thats a wonderful example above...and it works...but it stalls our program.  The Win32 API is called to start with our song...but it won't return until its finished....so our window won't appear, which isn't good :(  Even though the sound is cool :)

 

So to fix this we make a small adjustment, and add it a bit of threading, which allows us to start our song and carry on our normal windows graphics operations as usual without having to wait for the song to finish.

 

code: DownloadSourceCode

 

using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using System.Threading;

 

class SimpleWindow : System.Windows.Forms.Form

{

      private Thread oThread = null;

 

      [DllImport("winmm.dll")]

      public static extern long PlaySound(String lpszName, long hModule, long dwFlags);

 

      private string m_strSoundFile = "none.wav";

      public void PlayMySound()

      {

            if (m_strSoundFile.Length > 0)

            {

                  PlaySound(Application.StartupPath + "\\" + m_strSoundFile, 0, 0);

            }

            m_strSoundFile = "";

            oThread.Abort();

      }// End of PlayMySound(..)

 

      public void PlaySoundInThread(string wavefile)

      {

            m_strSoundFile = wavefile;

            oThread = new Thread(new ThreadStart(PlayMySound));

            oThread.Start();

      }// End of PlaySoundInThread(..)

 

      // Window constructor

      SimpleWindow()

      {

            PlaySoundInThread( "indianajones.wav" ); 

      }

 

      [STAThread]

      // Our program entry point.

      static void Main()

      {

            System.Windows.Forms.Application.Run(new SimpleWindow());

      }

}// End of SimpleWindow Class

 

Now the few extra lines for the ability to put your sound in a thread is well worth it...it means you can play a sound in the background while still doing your graphics....which means it won't look like your game or app has crashed each time theres a sound :)

 

 

 

 
Advert (Support Website)

 
 Visitor:
Copyright (c) 2002-2025 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.