Re: function PlaySound does not work
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Fri, 22 Sep 2006 21:12:28 -0400
"Susanne Wenzel" <wirdnichtgelesen@xxxxxxxxxxxxx> wrote in message news:1xqpw086ku6pa$.dlg@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi to everyone,
I have no idea whether this group is the right one for my problem so I just
try (and maybe I'm lucky:-)
I'm getting nowhere in trying to play a .wav-file using the function
Playsound. There's no sound, no noise to be heard when I use this function
in vb(a), actually excel-vba, same thing happens in delphi. It does not
matter if I step through the code one by one or start it via GUI. If I try
via Single-Step-Mode (I hope I've translated it somewhere near the correct
one) at least he (i.e. my computer) takes a little time until he proceeds
to the next step. As if he was doing something... The return-code I'm
getting is 1, so he thinks he's successful.
Nowhere did I cross out anything in the sound volume. If I start the
.wav-file by doubleclick in the explorer, I can hear the sound loud and
clearly, no problem.
I have no idea where to look to solve this little but nevertheless annoying
problem. I know from several other users that they had no problems using my
little function: they could start the .wav-file via vba and heard it.:-)
But I can't.:-(
Does anyone here have an idea or just a link for me?
Any help would be appreciated.
This is the code I used... to no avail:-(
Declare Function sndPlaySound32 Lib "winmm.dll" Alias "sndPlaySoundA"
(ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Public Sub Klang()
Dim a
a = sndPlaySound32("C:\WINNT\Media\tada.wav", 0)
Debug.Print a
End Sub
Your code looks OK except for the hard-coded path (and you didn't give your variable a type so it's a Variant, but that wouldn't affect the function working or not)....NEVER a good idea to hard-code paths. Under Windows XP, the default installation path is \WINDOWS...not \WINNT. And there's even no guarantee it's on the C: drive. For example, I have a multiboot system. Win98 is on C:. Win2000 is on I:. WinXP is on H:.
You need to use any of several methods to get the Windows directory. Once you have that, you can hard-code the rest of the path and filename. Here's one way to get the Windows directory that will work under all versions of Windows:
Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Public Function GetWinDir() As String
Dim sBuffer As String
Dim nBuffer As Long
Dim nBytes As Long
sBuffer = Space$(255)
nBuffer = Len(sBuffer)
nBytes = GetWindowsDirectory(sBuffer, nBuffer)
GetWinDir = Left$(sBuffer, nBytes)
End Function
Applying this to your code, you'd have something like this (air code):
Public Sub Klang()
Dim a As Long
Dim sWinDir As String
sWinDir = GetWinDir
If Right$(sWinDir,1) <> "\" Then 'always check for final backslash
sWinDir = sWinDir & "\"
End If
a = sndPlaySound32(sWinDir & "Media\tada.wav", 0)
Debug.Print a
End Sub
That should work under any version of Windows regardless of where Windows is installed. Of course, there's still the possibility that a user deleted the tada.wav file.
Additionally, I'd recommend declaring and using the constants for the uFlags parameter of the sndPlaySound function. Passing 0 doesn't really tell you anything when reading the code. To find out what 0 means, you probably need to look it up. However, if you declare the constant
Public Const SND_SYNC As Long = &H0 ' play synchronously (default)
and write this:
a = sndPlaySound32(sWinDir & "Media\tada.wav", SND_SYNC)
your code is clearer (i.e., it's clear you're playing the sound synchronously).
When you combine any flags for this parameter, it gets even worse if you don't use constants. Compare:
BAD:
a = sndPlaySound32(sWinDir & "Media\tada.wav", 18)
GOOD:
Private Const SND_NODEFAULT = &H2 ' silence not default, if sound not found
Private Const SND_NOSTOP = &H10 ' don't stop any currently playing sound
Private Const SND_SYNC As Long = &H0 ' play synchronously (default)
a = sndPlaySound32(sWinDir & "Media\tada.wav", SND_NODEFAULT Or SND_NOSTOP Or SND_SYNC)
Yes, the bad version is less typing, but laziness is no excuse for bad code. Even looking up the constants and figuring out what 18 means is not really a trivial task. You'll waste much more time doing that than if you just type the declarations and use the constants (especially since you can copy and paste the constants from the API Viewer).
--
Mike
Microsoft MVP Visual Basic
.
- Follow-Ups:
- Re: function PlaySound does not work
- From: Susanne Wenzel
- Re: function PlaySound does not work
- References:
- function PlaySound does not work
- From: Susanne Wenzel
- function PlaySound does not work
- Prev by Date: Re: function PlaySound does not work
- Next by Date: Re: Problems using APIs on a VISTA machine - Beta 2 August
- Previous by thread: Re: function PlaySound does not work
- Next by thread: Re: function PlaySound does not work
- Index(es):
Relevant Pages
|
|