Once it starts playing it will not stop till it finishes playing the file...
The best way to handle it is to use files which play only for 2-3 seconds...
Have made few changes
Place this in a module
Code:
Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _
(ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Public Const SND_ASYNC As Integer = &H1
Place this in every form/MDI Child load event
Code:
Private Sub Form_Load()
Dim lngRet As Long, strSNDFile As String
'~~> Change this part for every Form to the respective file...
strSNDFile = "c:\windows\media\explode.wav"
lngRet = PlaySound("SystemStart", 0, SND_ASYNC)
End Sub
Now if you see that whenever you open a form, the sound which you have specified will play...
By the way, if you have noticed that in the above code I have added one line...
Public Const SND_ASYNC As Integer = &H1
This "SND_ASYNC" plays the sound asynchronously i.e it moves to the next line of code immediately after starting to play the sound and have it play in the background....
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
Option Explicit
' To hear a sound for an event, the event must have a sound file
' associated with it in the users windows sound panel otherwise it will not play.
' If a sound is not avalable SND_NODEFAULT will return nothing, otherwise a beep will be heard.
Const SND_ALIAS_SYSTEMASTERISK As String = "SystemAsterisk"
Const SND_ALIAS_SYSTEMDEFAULT As String = "SystemDefault"
Const SND_ALIAS_SYSTEMEXCLAMATION As String = "SystemExclamation"
Const SND_ALIAS_SYSTEMEXIT As String = "SystemExit"
Const SND_ALIAS_SYSTEMHAND As String = "SystemHand"
Const SND_ALIAS_SYSTEMQUESTION As String = "SystemQuestion"
Const SND_ALIAS_SYSTEMSTART As String = "SystemStart"
Const SND_ALIAS_SYSTEMWELCOME As String = "SystemWelcome"
Const SND_ALIAS_YouGotMail As String = "MailBeep"
' playsound Params
Const SND_LOOP = &H8
Const SND_ALIAS = &H10000
Const SND_NODEFAULT = &H2 ' silence if no sound associated with event
Const SND_ASYNC = &H1 ' play async (don't freeze program while sound is playing)
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()
' loop "you got mail" sound until stopped!
PlaySound SND_ALIAS_YouGotMail, vbNull, SND_ALIAS Or SND_NODEFAULT Or SND_ASYNC Or SND_LOOP
End Sub
Private Sub Command2_Click()
PlaySound "SystemExclamation", vbNull, SND_ALIAS Or SND_NODEFAULT Or SND_ASYNC
End Sub
Private Sub Command3_Click()
PlaySound "SystemAsterisk", vbNull, SND_ALIAS Or SND_NODEFAULT Or SND_ASYNC
End Sub
Private Sub Command4_Click()
' by default windows XP doesn't have a sound associated with the question event,
' the user neeeds to set it to a file in the sound control panel.
PlaySound "SystemQuestion", vbNull, SND_ALIAS Or SND_NODEFAULT Or SND_ASYNC
End Sub
Private Sub Command5_Click()
PlaySound SND_ALIAS_SYSTEMDEFAULT, vbNull, SND_ALIAS Or SND_NODEFAULT Or SND_ASYNC
End Sub
Private Sub Command6_Click()
'Stop sound
PlaySound vbNullString, ByVal 0&, SND_NODEFAULT
' or PlaySound vbNullString, vbNull, SND_NODEFAULT
End Sub
Last edited by Edgemeal; Oct 29th, 2009 at 05:50 PM.
"SND_NODEFAULT" (Value : &H2)
It is used if the specified sound cannot be found, terminate the function with failure instead of playing the System Default sound. If this flag is not specified, the System Default sound will play if the specified sound cannot be located and the function will return with success.
This is the complete Flags table
Flags Table
===========
SND_ALIAS = &H10000: lpszName is a string identifying the name of the system event sound to play.
SND_ALIAS_ID = &H110000: lpszName is a string identifying the name of the predefined sound identifier to play.
SND_APPLICATION = &H80: lpszName is a string identifying the application-specific event association sound to play.
SND_ASYNC = &H1: Play the sound asynchronously -- return immediately after beginning to play the sound and have it play in the background.
SND_FILENAME = &H20000: lpszName is a string identifying the filename of the .wav file to play.
SND_LOOP = &H8: Continue looping the sound until this function is called again ordering the looped playback to stop. SND_ASYNC must also be specified.
SND_MEMORY = &H4: lpszName is a numeric pointer refering to the memory address of the image of the waveform sound loaded into RAM.
SND_NODEFAULT = &H2: If the specified sound cannot be found, terminate the function with failure instead of playing the SystemDefault sound. If this flag is not specified, the SystemDefault sound will play if the specified sound cannot be located and the function will return with success.
SND_NOSTOP = &H10: If a sound is already playing, do not prematurely stop that sound from playing and instead return with failure. If this flag is not specified, the playing sound will be terminated and the sound specified by the function will play instead.
SND_NOWAIT = &H2000: If a sound is already playing, do not wait for the currently playing sound to stop and instead return with failure.
SND_PURGE = &H40: Stop playback of any waveform sound. lpszName must be an empty string.
SND_RESOURCE = &H4004: lpszName is the numeric resource identifier of the sound stored in an application. hModule must be specified as that application's module handle.
SND_SYNC = &H0: Play the sound synchronously -- do not return until the sound has finished playing.
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
instead of vbnullstring i wrote "", and it didn't work of course
until i decided to go by edge advice, and wrote vbnullstring
and suddenly everything start to work as expected
so i checked in the help
and i found this
vbNullString String having value 0 Not the same as a zero-length string (""); used for calling external procedures
i think this can also solve me another problem with calling API
now one thing still left,
the playsound for each child doesn't work.
though if it will not, it also good, but i prefer to know if this is possible, and how
The answer to your question is Here
And here
C:\Program Files\Microsoft Visual Studio\MSDN98\98VSa\1033\SAMPLES\VB98
Please go to the "Thread Tools" menu at the top of this Thread, and click "Mark Thread Resolved" when you have your answer.
So I can fine the answer when I need it.
So please install VB6 service pack 6 as its the latest and last supported service pack MS will ever make. http://support.microsoft.com/kb/q198880/ I'm sure this will fix your issue
The only reason some people get lost in thought is because it’s unfamiliar territory. —Paul Fix
hay man, i'm really glad you tried to help,
and i really appreciate this.
sorry that i didn't explained myself,
i am looking after a way, to play from 2 different childs, at the same time,
so i can hear for example 2 waves playing at the same time.
the way you show, is what i know already,
is when you play a sound, any previuos sound, is turned off.
hay man, i'm really glad you tried to help,
and i really appreciate this.
sorry that i didn't explained myself,
i am looking after a way, to play from 2 different childs, at the same time,
so i can hear for example 2 waves playing at the same time.
the way you show, is what i know already,
is when you play a sound, any previuos sound, is turned off.
Not with PlaySound
But look here
Originally Posted by Jacob Roman
The PlaySound API only plays one sound at a time and is very limited of its capabilities. You get a lack of control. So if you are making a game or a music app, it is better to use DirectSound or WAVEMIX32, for more control over your sound, and allow more than one sound to play at the same time. Here are some tutorials on DirectSound:
The answer to your question is Here
And here
C:\Program Files\Microsoft Visual Studio\MSDN98\98VSa\1033\SAMPLES\VB98
Please go to the "Thread Tools" menu at the top of this Thread, and click "Mark Thread Resolved" when you have your answer.
So I can fine the answer when I need it.
So please install VB6 service pack 6 as its the latest and last supported service pack MS will ever make. http://support.microsoft.com/kb/q198880/ I'm sure this will fix your issue
The only reason some people get lost in thought is because it’s unfamiliar territory. —Paul Fix
hay man, i'm really glad you tried to help,
and i really appreciate this.
sorry that i didn't explained myself,
i am looking after a way, to play from 2 different childs, at the same time,
so i can hear for example 2 waves playing at the same time.
the way you show, is what i know already,
is when you play a sound, any previuos sound, is turned off.
Sorry, I misunderstand, but... however your initial request was: how to stop the playsound when it is in loop that's quite different from what you ask now!
Then you must to use MCI.
As example, you can start by this project:
C:\Program files\Microsoft Visual Studio\MSDN98\98VS\1033\SAMPLES\VB98\MCI
Open the prject MCITest and make some changes:
1.
Open frmMCITest form, in the routine below Private Sub AI_WAVEAUDIO_Click()
insert this statements
Code:
New_Wave
Exit Sub
2.
In same form, add this new routine:
Code:
Public Sub New_Wave()
DialogCaption = "Audio Wave - "
Dim fWave As frmWave
Set fWave = New frmWave
fWave.Caption = "Audio Wave"
frmOpenDlg.dlgOpenFile.Filter = "File Wave (*.wav)|*.wav"
fWave.mciWave.DeviceType = "WaveAudio"
fWave.Show
End Sub
That allow you to open two or more instances of frmWave.
3.
Open frmWave form, and in the routine:
Private Sub AI_OPEN_Click()
Comment the lines that close the wave device:
' If Not mciWave.Mode = vbMCIModeNotOpen Then
' mciWave.Command = "Close"
' End If
4. Run the project 5. from main menu click 2 times to Audio_Wave menu item: this open 2 instances of frmWave (move one form to see both) 6. in both instance of frmWave open a different WAVE file 7. finally, start play both the frmWave
thanks for that, so for that i was looking for,
for an answer that this is can't be done with playsound.
i can't use MCI because i need to play my waves in loop,
(they're very short, only one cycle waveform, to use later in a synth),
and MCI doesn't support that (i think) that's why i needed PlaySound.
EDIT: but maybe with MCI API it's possible to play in loop ?
Last edited by whatsup; Nov 8th, 2009 at 07:22 AM.
thanks for that, so for that i was looking for,
for an answer that this is can't be done with playsound.
i can't use MCI because i need to play my waves in loop,
(they're very short, only one cycle waveform, to use later in a synth),
and MCI doesn't support that (i think) that's why i needed PlaySound.
EDIT: but maybe with MCI API it's possible to play in loop ?
The answer to your question is Here
And here
C:\Program Files\Microsoft Visual Studio\MSDN98\98VSa\1033\SAMPLES\VB98
Please go to the "Thread Tools" menu at the top of this Thread, and click "Mark Thread Resolved" when you have your answer.
So I can fine the answer when I need it.
So please install VB6 service pack 6 as its the latest and last supported service pack MS will ever make. http://support.microsoft.com/kb/q198880/ I'm sure this will fix your issue
The only reason some people get lost in thought is because it’s unfamiliar territory. —Paul Fix
is there any way to get a handle on that operation whereby I could kill the operation ?
If one is playing multiple sounds, one could put each handle (if available) into an array where it can be retrieved to kill the particular sound. My problem is that when I close the form that is playing the sound, using Unload Me, the sound keeps playing.
Will appreciate any info on availability of a handle to identify the particular sound that would allow killing the sound.