Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Play system sounds 1

Status
Not open for further replies.

SavantMan

Programmer
Joined
Apr 17, 2002
Messages
165
Location
US
I am developing a small messaging app as my first foray into .net, so if I'm asking an embarassingly simple question, I appologize. (have checked docs/forums here and can't find an answer). I simply want to play a system sound (asterisk) when a user recieves a message, but can't find any call to do this. Anyone out there have an idea of what's involved?
 
'Not sure if there are any vb.net classes to handle it, but its pretty easy with the windows api:

Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_ALIAS = &H10000

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Call PlaySound("CriticalStop", 0, SND_ASYNC Or SND_ALIAS)

End Sub

'To play different sounds check out the sounds section of the control panel for the various sound names. Just replace the string "CriticalStop" with the name of the windows sound you want to use.

If there is a .NET class which handles this I would liek to know?
 
Calling the PlaySound API seems to play the default sound regardless of what system sound I specify.

Am I missing something ?
 
I just finally had time to get back to this, and yes the above does only play the default sound. However, it's a very minor change.

Instead of:
Call PlaySound("Asterisk", 0, SND_ASYNC Or SND_ALIAS)

Use
Call PlaySound("SystemAsterisk", 0, SND_ASYNC Or SND_ALIAS)

I don't beleive that all system sounds are supported in this fashion, but a number of them are.
 
This is what I use to play wav files in VB.net ... simply call PlaySound(SoundFile) with sound file being the path and name of the sound file.

Becca

Module soundMod

#Region " *** Classes *** "

Public Class SoundClass
Declare Auto Function PlaySound Lib "winmm.dll" (ByVal name _
As String, ByVal hmod As Integer, ByVal flags As Integer) As Integer
' name specifies the sound file when the SND_FILENAME flag is set.
' hmod specifies an executable file handle.
' hmod must be Nothing if the SND_RESOURCE flag is not set.
' flags specifies which flags are set.

' The PlaySound documentation lists all valid flags.
Public Const SND_SYNC = &H0 ' play synchronously
Public Const SND_ASYNC = &H1 ' play asynchronously
Public Const SND_FILENAME = &H20000 ' name is file name
Public Const SND_RESOURCE = &H40004 ' name is resource name or atom

Public Sub PlaySoundFile(ByVal filename As String)
' Plays a sound from filename.
PlaySound(filename, Nothing, SND_FILENAME Or SND_ASYNC)
End Sub
End Class

#End Region

#Region " *** All Subs *** "

Public Sub playSound(ByVal wavfile As String)
Dim SoundInst As New SoundClass
SoundInst.PlaySoundFile(wavfile)
End Sub

#End Region

End Module
 
Thanks for that Rebecca.
star.gif


Starting the move to .Net by rewriting a VB6 game I wrote for my kids. Saved me some time.

-dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top