Tag Archives: sndPlaySound

How to Play Wave Files Easily with VB 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
 

Playing Wave files with Visual Basic (6.0 and older versions) is quite easy using the Windows APIs. The API you would be interested in using to play wav files is the: PlaySound – API Call. Below is the Constants and Function which should be put in a Module or Declaration section of your project…

   '
    'These Constants and API call was copied from the API-Guide program.

    Private Const SND_APPLICATION = &H80 ' look for application specific association

    Private Const SND_ALIAS = &H10000 ' name is a WIN.INI [sounds] entry

    Private Const SND_ALIAS_ID = &H110000 ' name is a WIN.INI [sounds] entry identifier

    Private Const SND_ASYNC = &H1 ' play asynchronously

    Private Const SND_FILENAME = &H20000 ' name is a file name

    Private Const SND_LOOP = &H8 ' loop the sound until next PlaySound

    Private Const SND_MEMORY = &H4 ' lpszSoundName points to a memory file

    Private Const SND_NODEFAULT = &H2 ' silence not default, if sound not found

    Private Const SND_NOSTOP = &H10 ' don't stop any currently playing sound

    Private Const SND_NOWAIT = &H2000 ' don't wait if the driver is busy

    Private Const SND_PURGE = &H40 ' purge non-static events for task

    Private Const SND_RESOURCE = &H40004 ' name is a resource name or atom

    Private Const SND_SYNC = &H0 ' play synchronously (default)

    Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal _
lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long

Below is how to use the Playsound function to play a wave file…

'
'This will simply play a wave sound from a file location and aSync so it uses
'a thread other than  your applications thread and will not keep your application
'from responding till its done, like playing Sync would do.
'
'The flag: SND_FILENAME I believe is the default for the PlaySound function.
'You can execute'that flag if you want to make sure it plays the sound file fine.
'
PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC)
'
'
'IF you want to use more than 1x flag with the api call, then just call it like below....
'
'This will play a wave file async and loop it over and over till you call the PlaySound
'Function again.
PlaySound("c:\windows\media\tada.wav", 0, SND_ASYNC Or SND_LOOP)

And that all there is to it to call and use the Playsound function to play a wave file.

But what if you wanted to stop a wave file that is already playing? That is taken care of below…

'
'This will call the Playsound function to execute and play nothing which will interupt and stop the previous wave file from playing.

PlaySound(vbNullString, 0, 0)

And that is all you have to do to play wav sound files with Visual Basic 6.0. Hope you found this useful 🙂

Jason

 

Minor Revision: 2014

How to Easily Play Wave Files with Visual Basic.NET and Newer

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

It’s actually quite easy to play wave files (.wav waveform based sound files) using just alittle code in some cases. The .NET Framework 2.0, 3.5, 4.0, and higher has a managed wrapper around the PlaySound API.

The easiest way to play wave files with Visual Basic 2005, VB 2008, VB 2010, and newer is as shown below…

    'Play a wave sound file from a specified location.
    My.Computer.Audio.Play("mysoundlocation.wav")

    'Stop the file from playing.
    My.Computer.Audio.Stop()

    'Play a Windows system sound.
    My.Computer.Audio.PlaySystemSound(Media.SystemSounds.Beep)

The code above simply used the MY Interface for quick access to the Audio class to play a file, stop the playback, and then playback a system sound.


What about playing wave sound files with Visual Basic.NET 2002 and Visual Basic.NET 2003?

Continue reading