dtucker
asked on
Playing a .wav file in vb6
This should be an easy question. I am making a small program for playing .wav files. I am using the API
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
and the constants
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Now I have no problem playing a wav file, but I cannot figure out how to stop playing a wave file while it's playing.
and I would like to know how to play another wav file while another is playing(It would stop playing the first wav and start playing the one I click on)
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
and the constants
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Now I have no problem playing a wav file, but I cannot figure out how to stop playing a wave file while it's playing.
and I would like to know how to play another wav file while another is playing(It would stop playing the first wav and start playing the one I click on)
I think you need to add the SND_NODEFAULT flag in there. Otherwise the default sound is played when you don't specify a sound. But I haven't played with that API in awhile... =)
This works for me to stop and start wav files:
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_NODEFAULT = &H2
Private Const SND_MEMORY = &H4
Private Const SND_LOOP = &H8
Private Const SND_NOSTOP = &H10
Private Sub Command1_Click()
sndPlaySound 0, SND_ASYNC
sndPlaySound "C:\Test.wav", SND_ASYNC Or SND_NODEFAULT
End Sub
Private Sub Command2_Click()
sndPlaySound 0, SND_ASYNC
sndPlaySound "C:\Tada.wav", SND_ASYNC Or SND_NODEFAULT
End Sub
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Const SND_ASYNC = &H1
Private Const SND_NODEFAULT = &H2
Private Const SND_MEMORY = &H4
Private Const SND_LOOP = &H8
Private Const SND_NOSTOP = &H10
Private Sub Command1_Click()
sndPlaySound 0, SND_ASYNC
sndPlaySound "C:\Test.wav", SND_ASYNC Or SND_NODEFAULT
End Sub
Private Sub Command2_Click()
sndPlaySound 0, SND_ASYNC
sndPlaySound "C:\Tada.wav", SND_ASYNC Or SND_NODEFAULT
End Sub
It is a good idea to create a class to play the sound, in this way you can be sure that you can stop the sound before your application closes. I think Idle_Mind was right about how to do that.
If your app closes while a sound is still playing there is a change that you may get GP Faults
Dim MyPlayer As clsPlayer
Set MyPlayer = New clsPlayer
MyPlayer.PlayStart "C:\test.wav"
And later when needed:
MyPlayer.PlayStop
When MyPlayer is disposed or goes out of scope (if you forgot) it will clean itself up without GPF
Hope this helps:~)
-------clsPlayer.cls
' needs defs here as above
Public Sub PlayStart(psFileName as string)
sndPlaySound psFileName, SND_ASYNC Or SND_NODEFAULT
End Sub
Public Sub PlayStop()
sndPlaySound 0, SND_NODEFAULT
End Sub
Private Sub Class_Terminate()
PlayStop
End Sub
If your app closes while a sound is still playing there is a change that you may get GP Faults
Dim MyPlayer As clsPlayer
Set MyPlayer = New clsPlayer
MyPlayer.PlayStart "C:\test.wav"
And later when needed:
MyPlayer.PlayStop
When MyPlayer is disposed or goes out of scope (if you forgot) it will clean itself up without GPF
Hope this helps:~)
-------clsPlayer.cls
' needs defs here as above
Public Sub PlayStart(psFileName as string)
sndPlaySound psFileName, SND_ASYNC Or SND_NODEFAULT
End Sub
Public Sub PlayStop()
sndPlaySound 0, SND_NODEFAULT
End Sub
Private Sub Class_Terminate()
PlayStop
End Sub
Private Const SND_ASYNC = &H1
Private Const SND_FILENAME = &H20000
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 Form_Load()
PlaySound "C:\WINDOWS\MEDIA\TADA.WAV ", ByVal 0&, SND_FILENAME Or SND_ASYNC
End Sub
or
if you are interested in using the control here is the code to play a wav file
MMControl1.DeviceType = "WaveAudio"
MMControl1.Command = "Stop"
MMControl1.FileName = "C:\mypath\mywav.wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"
Private Const SND_FILENAME = &H20000
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 Form_Load()
PlaySound "C:\WINDOWS\MEDIA\TADA.WAV
End Sub
or
if you are interested in using the control here is the code to play a wav file
MMControl1.DeviceType = "WaveAudio"
MMControl1.Command = "Stop"
MMControl1.FileName = "C:\mypath\mywav.wav"
MMControl1.Command = "Open"
MMControl1.Command = "Play"
Related question, just posted, 500 points:
https://www.experts-exchange.com/questions/21802757/programmatically-play-mp3-files-on-windows-w-C-net.html
https://www.experts-exchange.com/questions/21802757/programmatically-play-mp3-files-on-windows-w-C-net.html
ASKER
I tried the above suggestions but I still cannot get the sound to stop. If I click play the sound starts, If I click on a different wav it stops the first one and plays the second one.
Here is my bas file
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Public wFlags%
Public X
Public SoundName As String
This is my wave player subroutine
Sub WAVPlay(File)
wFlags% = SND_ASYNC Or SND_NODEFAULT
X = sndPlaySound(SoundName$, wFlags%)
End Sub
Here is my play the wav command
SoundName$ = Dir1.Path & "\" & File1.FileName
sndPlaySound (SoundName$), SND_ASYNC
and here is my stop command
sndPlaySound 0, SND_ASYNC Or SND_NODEFAULT
I don't want to use the MMControl, just the API.
If I exit the program while a wav is playing, it just continues to play until the end.
Here is my bas file
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" _
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Const SND_ASYNC = &H1
Public Const SND_NODEFAULT = &H2
Public Const SND_MEMORY = &H4
Public Const SND_LOOP = &H8
Public Const SND_NOSTOP = &H10
Public wFlags%
Public X
Public SoundName As String
This is my wave player subroutine
Sub WAVPlay(File)
wFlags% = SND_ASYNC Or SND_NODEFAULT
X = sndPlaySound(SoundName$, wFlags%)
End Sub
Here is my play the wav command
SoundName$ = Dir1.Path & "\" & File1.FileName
sndPlaySound (SoundName$), SND_ASYNC
and here is my stop command
sndPlaySound 0, SND_ASYNC Or SND_NODEFAULT
I don't want to use the MMControl, just the API.
If I exit the program while a wav is playing, it just continues to play until the end.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked perfectly, now I will open another question...How would I go about changing the volume
sndPlaySound 0, SND_ASYNC