user867621 user867621 - 3 months ago 13
Vb.net Question

vb.net 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

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

My.Computer.Audio.Play(location)

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:

Beep()

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

Comments