Sound

databases United Kingdom
  • 19 years ago

    How do I programatically play a sound file when a certain event occurs?

  • 19 years ago

    You can use the following code to play a sound file (.wav)


    Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long



    Dim SoundFile As String, Result As Long


    SoundFile = "C:\Windows\Media\Ding.wav"
    Result = sndPlaySound(SoundFile, 1)


    Hope this helps.

  • 19 years ago

    I appreciate your help, but I'm having difficulty getting this code to execute properly. I'm an intermediate VB user and I can't tell you I understand this code 100%. So what I did is copy and paste. However, I'm not too sure where to place this code. Outside an existing procedure or inside? Also, shouldn't the declared function have some sort of ending tag (End Sub or whatever)? It's mainly the first line that I can't quite grip.
    Thank you in advance.

  • 19 years ago

    Ok.


    This bit goes in General_Declarations (click general in the left combo of the code window, and declaractons in the right)


    Code:
    Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long


    This line of code tells vb that you want to use a windows internal function (API) to play the sound.


    Next, put this bit in the event procedure where you want to play the sound eg. a command_click() event or something.


    Code:
    Dim SoundFile As String, Result As Long


    SoundFile = "C:\Windows\Media\Ding.wav"
    Result = sndPlaySound(SoundFile, 1)



    Change the bit "C:\Windows\Media\Ding.wav" to the path of the sound file you want play. eg.

    Code:
    App.Path & "\sound.wav"
    or something.


    If you need anymore help let me know.


    Hope this works.

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“In order to understand recursion, one must first understand recursion.”