Click to See Complete Forum and Search --> : How do I play sounds?
Marble
Nov 23rd, 2000, 12:10 PM
Originally (before I found out about API), the only way that I could play sound files was to use the Multimedia Control. My code basically consisted of about four to five lines of code, just to play a simple sound file.
I'm very new to API, and I was just wondering if there was a simple way of playing sound files using an API call.
Thanks to anyone who can help.
Use the PlaySound api function.
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
After you've 'stopped' your MIDI, you should use the following statement to close it.
mciSendString "close myfile", 0, 0, 0
WP
Nov 26th, 2000, 02:53 AM
And what about DirectX?
or is it slower
WP
Vlatko
Nov 26th, 2000, 05:40 AM
DirectX (DirectSound) is more complex but not slower than API.
You should only use DirectSound if you are planning to use other DirectX function in your App. Otherwise, you'll have to include the Type Library all for a couple sounds.
Wak
Nov 26th, 2000, 10:16 PM
The easiset API for playing conventional sounds is:
code:
option explicit
Private Declare Function sndPlaySound _
Lib "winmm.dll" Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long _
) As Long
Private Sub Command1_Click()
sndPlaySound("C:\windows\media\start.wav", &H1)
End Sub
Hope it Works
elwyn5150
Dec 5th, 2000, 01:43 AM
How do I stop a looping sound file from playing without playing another sound file?
I've looked up the VB API at http://www.vbapi.com/ref/p/playsound.html and the example:
retval = PlaySound("", 0, SND_PURGE OR SND_NODEFAULT) doesn't work on my program.
NOTE: I also even set the values of SND_PURGE and SND_NODEFAULT to what they are meant to be.
To stop a sound:
Private Sub Command2_Click()
PlaySound "C:\WavFile.wav", 0&, &H4
End Sub
lvramanan
Dec 18th, 2000, 07:36 PM
All the above API's will play .wav or .midi. Those API's will not play MP3. Is it Possible to play MP3 files using DirectX or DirectSound. How will be the Quality of the Sound. I'm having DirectX 7a. If it is possible to play, please send me the coding.
Originally posted by Marble
Originally (before I found out about API), the only way that I could play sound files was to use the Multimedia Control. My code basically consisted of about four to five lines of code, just to play a simple sound file.
I'm very new to API, and I was just wondering if there was a simple way of playing sound files using an API call.
Thanks to anyone who can help.
I am trying create a CD menu which plays sounds, i can get the form to play the sound using a API call but the problem i have is that the sound plays without the form being displayed. the form only appears after the sound stops.
i think i need to create the EXE as a multi threeded application but i dont know how.
DJDANNYK
Dec 20th, 2000, 12:22 PM
To play an mp3 file instead use this code
Public Function PlayMP3(Filename As String)
Dim dwreturn As Long
Dim ret As String * 128
Dim tmp As String * 255
Dim lenShort As Long
Dim ShortPathAndFie As String
'send a filename to the function
If Dir(FileName) = "" Then
msgbox "Error with input file"
Exit Function
End If
lenShort = GetShortPathName(FileName, tmp, 255)
ShortPathAndFie = Left$(tmp, lenShort)
glo_hWnd = hwnd
cmdToDo = "open " & ShortPathAndFie & " type MPEGVideo Alias mpeg"
dwreturn = mciSendString(cmdToDo, 0&, 0&, 0&)
If dwreturn <> 0 Then 'not success
mciGetErrorString dwreturn, ret, 128
MsgBox ret, vbCritical
Exit Function
End If
'then to play just send this
dwreturn = mciSendString("play mpeg", 0&, 0&, 0&)
If dwreturn <> 0 Then
mciGetErrorString dwreturn, ret, 128
MsgBox ret, vbCritical
Exit Function
End If
End Sub
This will also play wavs...
Originally posted by Wak
The easiset API for playing conventional sounds is:
code:
option explicit
Private Declare Function sndPlaySound _
Lib "winmm.dll" Alias "sndPlaySoundA" ( _
ByVal lpszSoundName As String, _
ByVal uFlags As Long _
) As Long
Private Sub Command1_Click()
sndPlaySound("C:\windows\media\start.wav", &H1)
End Sub
Hope it Works
sndPlaySound has been superseded by PlaySound
Marble
Dec 22nd, 2000, 03:54 PM
Thanks for your help everyone! I didn't expect this many replies :)
Wak
Dec 23rd, 2000, 04:48 AM
[quote]
sndPlaySound has been superseded by PlaySound
[/qoute]
I realize what your saying and all, but the guy just wants to play a sound. What really confused me when I started
out, is when you have a really simple question, and people
swamp you with all this crap, which makes you even more confused. He said he's just new to API, so why give him
an unecessarily hard API function when the easy one will do
the trick. I mean, he only wants to play a sound. Probably for a mouse event or something. NO OFFENCES.
Thanx :):)
How is PlaySound "an unecessarily hard API function"? They're practically the same, except PlaySound allows you to play through a module. If you don't intend to use this argument this simply omit it.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.