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)
dtuckerAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
This worked for me...notice I'm using PlaySound()...not sndPlaySound()...

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_PURGE = &H40

Private Sub Command1_Click()
    PlayWav "C:\Documents and Settings\Michael\My Documents\007.wav"
End Sub

Private Sub Command2_Click()
    StopWav
End Sub

Private Sub PlayWav(ByVal fileName As String)
    PlaySound fileName, ByVal 0&, SND_ASYNC Or SND_NODEFAULT
End Sub

Private Sub StopWav()
    PlaySound vbNullString, ByVal 0&, SND_PURGE
End Sub
0
 
vinnyd79Commented:
Have you tried:

sndPlaySound 0, SND_ASYNC
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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...  =)
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
vinnyd79Commented:
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
0
 
inthedarkCommented:
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
0
 
Naveen SwamyYash Infinite Solutions Private LimitedCommented:
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"

0
 
SweatCoderCommented:
0
 
dtuckerAuthor Commented:
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.
0
 
dtuckerAuthor Commented:
That worked perfectly, now I will open another question...How would I go about changing the volume
0
All Courses

From novice to tech pro — start learning today.