mPlayWAV (Play Audio in your Application)

Well a thew months ago i downloaded a application which had a nice sound effect You’ve logged in so i thought let’s do a little function to help myself out.
So i started it it didn’t took long and i was done after i knew on how to play WAV files, but it was more Interesting for me to play them from Resource Files so here you go i hope it comes in use for you people i know it’s a little messy but it was 01:58AM and i hadn’t slept for days xD.

Option Explicit
‘—————————————————————————————
‘ Module : mPlayWAV
‘ DateTime : 20.04.2010 01:58AM
‘ Author : Kreshnik Hasanaj
‘ Mail : kresha7@hotmail.com
‘ Purpose : Play a wav file from Resource or File
‘ Usage : At your own risk
‘ Requirements: None
‘ Distribution: You can freely use this code in your own
‘ applications, but you may not reproduce
‘ or publish this code on any web site,
‘ online service, or distribute as source
‘ on any media without express permission.

‘—————————————————————————————

‘Used API declaration
Private Declare Function sndPlaySound _
Lib “winmm.dll” _
Alias “sndPlaySoundA” (ByVal lpszSoundName As String, _
ByVal uFlags As Long) As Long
Private Declare Function PlaySoundData _
Lib “winmm.dll” _
Alias “PlaySoundA” (lpData As Any, _
ByVal hModule As Long, _
ByVal dwFlags As Long) As Long

‘Enumuration to select the way of playing the file
Public Enum PlayType
WAVFromRes = 0
FromFile = 1
End Enum

‘Flags used by API
Private Const SND_ASYNC = &H1
Private Const SND_NODEFAULT = &H2
Private Const SND_MEMORY = &H4

‘Buffer for the Resource WAV File
Dim ResData() As Byte

Public Function PlayWAVSound(PType As PlayType, _
Optional ResID As Integer = 0, _
Optional ResType As String = vbNullChar, _
Optional WavPath As String = vbNullChar) As Integer
‘Simple error handling
On Error GoTo errHandle:

Const Flags = SND_MEMORY Or SND_ASYNC Or SND_NODEFAULT

‘Are any parameters used ?
If ResID = 0 And WavPath = “” Then
Exit Function

Else

‘What king of playing type is selected ?
Select Case PType

Case 0
ResData = LoadResData(ResID, ResType)
PlayWAVSound = PlaySoundData(ResData(0), 0, Flags)

Case 1
PlayWAVSound = sndPlaySound(WavPath, SND_ASYNC Or SND_NODEFAULT)

End Select

End If

Exit Function
‘Error handling
errHandle:
MsgBox “Error Code: ” & Err.Number & vbCrLf & “Error Description: ” & _
Err.Description, vbCritical, “Error”

End Function

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s