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

15 thoughts on “How to Play Wave Files Easily with VB 6.0

  1. Chris Hekimian

    Thank you Jason for providing this excellent solution in a way that is easy to understand and implement. I looked lots of other places but your site was the last place I needed to visit. Thanks!!!

    Reply
  2. burcu

    Why i can’t play long musics. Could you please help me to play?

    For example i can’t play the music in “c:\windows\media\flourish.wav”

    Reply
  3. Ivan Stern

    Hi Jason;

    Looks like a good solution for playing sounds, but I can’t get it to work. I’m trying to start some games programming with some standard Windows sounds. I keep getting an “Expected =” error, as if it’s looking for a function return.

    Any help?

    Ivan Stern

    Reply
  4. Doug

    Is there anyway to play the sound while the application is running? The way I have it now, the sound will play fine but the application doesn’t start until the sound is over.

    Reply
  5. mina

    Thanks.I used it but when I start debugging it has error that ” be sure a sound file exists at the specified location” while it exists ! what should I do?
    I have another question too..can you help me what is routing algorithm code in VB.Net?

    Reply
  6. Jason Post author

    Hi, You need to provide the exact path to the .Wav file you want to play. The soundplayer in vb.net and the playsound api ONLY supports Wave files. Not MP3 or anything else like that. .Wav files only.

    Your other question I don’t have any helpful information to give you. Try posting a question on vbforums.com or search planetsourcecode.com 🙂

    Jason

    Reply
  7. alen

    Pliz help I’m new to vb and I have created a simple program that reads name when u click a certain command button but I just can’t play a wav file from my local drive that contains the names I hav recorded. Pliz help I’m using vb 6.0 on a machine that is running windows 7

    Reply
  8. Jason Post author

    Hi, you need to make sure your specifying the exact path to the wave file. Remember, it HAS to be a wave file (.wav). MP3 will Not play with this api code.!

    Jason

    Reply
  9. Don

    I keep getting a compile error on the SoundPlay function.

    VB is looking for an expected =

    What’s up with this error

    Reply

Leave a Reply

Your email address will not be published.