user867621 user867621 - 1 year ago 78 Question Is there a simple way to add a sound to a button click

Is there a simple way of doing this

I found this online and I am getting an error

Declare Function apisndPlaySound Lib "winmm" Alias "sndPlaySoundA" _
(ByVal filename As String, ByVal snd_async As Long) As Long

Function PlaySound(ByVal sWavFile As String)
' Purpose: Plays a sound.
' Argument: the full path and file name.

If apisndPlaySound(sWavFile, 1) = 0 Then
MsgBox("The Sound Did Not Play!")
End If
End Function

A call to PInvoke function 'WindowsApplication3!WindowsApplication3.Module1::apisndPlaySound' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

Answer Source

Use the SoundPlayer class, in you can access it with the My keyword as in:


But if its for a button click, you'll get a perf bottleneck (by maxing out IO) reading a *.wav file everytime a user clicks the button. In that case I recommend using:


Also as a general rule of thumb, KISS by investigating/researching methods in the .Net framework first, before resorting to the Win32 API's. HTH

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download