and if there is a way to call it from different forms
and play for each form different sound, please show me an example code
thank you very much.
Printable View
and if there is a way to call it from different forms
and play for each form different sound, please show me an example code
thank you very much.
How to START play?
Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
PlaySound TempFileName, ByVal 0&, SND_FILENAME Or SND_ASYNC Or SND_LOOP Or SND_APPLICATION
Place this in a moduleQuote:
play for each form different sound
Place this in the form load event of your each form....vb Code:
Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" _ (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
vb Code:
Private Sub Form1_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, &H0) End Sub
thank you
what i try to guess that first i have to play this line in form_load
then every file i wantCode:lngRet = PlaySound("SystemStart", 0, &H0)
i tried it
-first it make the windows start sound
-second it doesn't make a sound for each form
btw i'm talking about MDI childs
-third what about my first question
how to make it shut up
i tried this
but it doesn't workCode:PlaySound "", 0, 0
i have to close VB for the sound to turn off
very confused :(
i want to use this instead of direct X because direct X 8 needs installation
on Vista and above
anyone ?
Sorry.. had missed this post...
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
Place this in every form/MDI Child load eventCode: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
Now if you see that whenever you open a form, the sound which you have specified will play...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
By the way, if you have noticed that in the above code I have added one line...
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....Quote:
Public Const SND_ASYNC As Integer = &H1
One of these should stop the current sound
PlaySound vbNullString, 0&, SND_NODEFAULT
or,
PlaySound vbNullString, vbNull, SND_NODEFAULT
or
PlaySound vbNullString, 0&, 0&
'''''''
Code: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
@ Edge
"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
Quote:
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.
sorry guys, it was my fault, i'm shame
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
i think this can also solve me another problem with calling APIQuote:
vbNullString String having value 0 Not the same as a zero-length string (""); used for calling external procedures
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
thanks for great help
Have you put it in each child form's load event mentioning the relevant file name?
i put in the form_load event
this line
and in other sub, where i need to play, i put this lineCode:playsound "SystemStart", 0, SND_ASYNC
Code:PlaySound TempFileName, 0, SND_FILENAME Or SND_ASYNC Or SND_LOOP
so ?
no one know the answer for that ?
maybe an example code with MDI and 2 childs to demo how to use playsound to play different wave for each child at the same time ?
Here you go :wave:
how to stop the playsound when it is in loop
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.
Nice mod :thumb:
Not with PlaySound
But look here
DirectX
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
2.Code:New_Wave
Exit Sub
In same form, add this new routine:
That allow you to open two or more instances of frmWave.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
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
Take note that the above project use MCI32.OCX, however you can achieve same result via API, learning MCI functions:
http://msdn.microsoft.com/en-us/libr...74(VS.85).aspx
Example here
http://allapi.mentalis.org/apilist/mciSendCommand.shtml
:wave:
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 ?
Search is your frend ;)
Search: Keyword(s): DirectSound ; Forum: Visual Basic 6 and Earlier and child forums
When I start playing a sound using -
RetVal = PlaySound("C:\HelloWorld.wav", 0&, &H20000)
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.
boblite....start your own thread...this one is real, real old.
thanks for the heads up, Sam