i want to play a wavefile in the background when ever my application get loaded and stop the wavefile when ever my application get unloaded.
who to do this? please help me.
Printable View
i want to play a wavefile in the background when ever my application get loaded and stop the wavefile when ever my application get unloaded.
who to do this? please help me.
In the general declarations:
Private Const SND_ASYNC = &H1
Private Const SND_NODEFAULT = &H1
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
and in the form_load:
strWavFile = App.Path & "\mywavfile.wav"
Call sndPlaySound(strGameoverSound, SND_ASYNC And SND_NODEFAULT)
hope it helps!
sorry, i was pasting from a game i wrote.
the last one "strGameOverSound" should be "strWavFile"
Do not use sndPlaySound. Use the PlaySound api function instead.
Code:Private Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Sub Command1_Click()
PlaySound "C:\MyFile.wav", 0&, &H1
End Sub
Use this code to play a sound when the program starts and stop it when the program unloads.
Code:Option Explicit
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_NODEFAULT = &H2
Private Const SND_FILENAME = &H20000
Private Const sFile = "C:\Windows\Media\Ctmelody.wav" ' You might want to change this :rolleyes:
Private Sub Form_Load()
Call PlaySound(sFile, 0, SND_ASYNC Or SND_NODEFAULT Or SND_FILENAME)
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Call PlaySound(vbNullString, 0, SND_ASYNC)
End Sub
sorry, what's the difference between sndPlaySound and Playsound?
Basically it added support for playing WAV's from resource files.
sndPlaySound is the 16-bit version, and PlaySound is the 32-bit version.
MSDN said, do not use the "obsolete" sndPlaySound in 32-bit programs, but use PlaySound instead. And you know how programmers get with MSDN... (MSDN says jump, programmers ask how high) :rolleyes:
Actually, you might want to consider using the WAV-in-resource-file solution. It is a way to pack your WAV in the program's EXE, instead of putting it in a separate file. It may be useful, depending on what kind of program you are working on.
Well actually the sndPlaySound located in winmm.dll is a 32-bit function.
It has been rewritten (or rather recompiled) for backward compatibility with 16-bit Windows program.
I just wanted to ******** that.
BTW Yonathan,
When MSDN tells me to jump I say - OK, if you give me some example code.
So I haven't jumped yet :)