Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PlaySound api doesn't work 3

Status
Not open for further replies.

OrthoDocSoft

Programmer
May 7, 2004
291
US
Folks,

I'm using the Playsound api call and it works fine on my development computer (running XP). When I intall in in my client computer (running 2000 professional, I think): no sound. I looked for registrations and components that might be missing in the compile, but nothing was clear. Any thoughts?

(here's the api)
Code:
Public Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As String, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long

Thanks,

Ortho

[lookaround] "you cain't fix 'stupid'...
 
Hi Ortho,

The code works OK as vba in Office 2K on Vista. Are you calling correctly? I used:
Code:
Sub Test()
Call PlaySound("C:\Windows\Media\chimes.wav", 0, 0)
End Sub
Cheers

[MS MVP - Word]
 
macropod, thanks.

here's the line that plays the api and the constants that feed them:

Code:
Public Const SND_ASYNC = &H1
Public Const SND_FILENAME = &H20000

PlaySound strAppPathway, ByVal 0&, _
        SND_FILENAME Or SND_ASYNC

I basically copied this code from someone's example, so I don't really know if its written correctly. I added a testing msgbox to show you what strAppPathway is, and that is (essentially):

Code:
C:\Program Files\MyApplication\media\mywav.wav

See any problems?

Thanks again,

Ortho


[lookaround] "you cain't fix 'stupid'...
 
Is winmm.dll registered on the client computer?


"Don't be irreplaceable. If you can't be replaced, you can't be promoted."
 
uh, don't know....

How do you tell, and how do you package that with your app for the times it is not?

Thanks,

Ortho (newbie)

[lookaround] "you cain't fix 'stupid'...
 
I'm at the client computer (it's my work computer -- I program at home) and I did a search for the file. It is present in folder system32.

Any other suggestions?

thanks,

Ortho

[lookaround] "you cain't fix 'stupid'...
 
As far as packaging goes, you can add it as an include file in Package & Deployment Wizard (if that's what you use).
Being on the machine and being registered may not be the same thing. This is such a common file that I can't imagine it unregistered. I never suggest messing in the registry, but you could look at the help files in regedit if you are curious...

"Don't be irreplaceable. If you can't be replaced, you can't be promoted."
 
>C:\Program Files\MyApplication\media\mywav.wav

Try using a resource file:

To place "mywav.wav" in a resource file

1-Click Add-Ins on ide menu
2-Click Add-In Manager
3-Select VB 6 Resource Editor
4-Double Click the Green Rubics Cube Icon in ide
5-Select Add-Custom Resource and browse to "mywav.wav" to add it
6-Expand the Custom folder and Double Click 101
7-Change "Custom" to "WAVE"



Code:
Private Declare Function PlaySound Lib "WINMM.DLL" Alias "PlaySoundA" (ByRef Sound As Any, _
     ByVal hLib As Long, ByVal lngFlag As Long) As Long 'BOOL
Const SND_ASYNC = &H1
Const SND_NODEFAULT = &H2
Const SND_MEMORY = &H4

Public Function PlayWaveSound() as Boolean
	Dim SoundByte as Byte
	On Error Goto PSError
	
        'Load SoundByte from Resource File
	SoundByte = LoadResData(101, "WAVE")
	
	PlaySound SoundByte(0), 0, SND_MEMORY Or SND_NODEFAULT Or SND_ASYNC
	PlayWaveSound = True
	Exit Function
PSError:
	PlayWaveSound = False
End Function
 
>winmm.dll registered

Why would you need to register a classic DLL?
 
Ted,

If you meant the physical volume knob on the computer, yes. Internet media files play fine and I can hear them. I suspect you mean something different, tho.

Thanks,

Ortho

[lookaround] "you cain't fix 'stupid'...
 
What happens if instead of

PlaySound strAppPathway, ByVal 0&, SND_FILENAME Or SND_ASYNC

you simply try

PlaySound strAppPathway, ByVal 0&, SND_ASYNC

 
strongm,

just tried your last suggestion in my development environ and it worked fine (why am I not surprised?). I will try it in the next few days in my client's environ and report the result. You obviously think it will improve the situation and I am banking on you....

Ortho

[lookaround] "you cain't fix 'stupid'...
 
>Dim SoundByte() As Byte

This sometimes causes problems resulting in application to crash. The reason is explained below.

The Byte array, SoundByte is declared using Dim at procedure level and passed to PlaySound API which plays the sound asynchronously (SND_ASYNC).

VB spares the memory used by the array as soon as the procedure is completed and the memory is made available for other purposes. However, the PlaySound function continues to read the wave data as the sound plays and expects the data to last till the sound is played completely. Since the memory is freed by VB, this causes access violation and may result in a crash, if the freed memory is used for other purposes.

Long time ago, I also had this problem when trying to embed some sounds in a game. Following are the workarounds.
[ol]
[li]Use [tt]Static SoundByte() As Byte[/tt] instead of [tt]Dim SoundByte() As Byte[/tt].[/li]
[li]Declare SoundByte() at module level, so it is not destroyed after the procedure is completed.[/li]
[li]Play the sound synchronously, PlaySound function will not return until the sound is played back completely.[/li]
[/ol]
I used static declaration.
 
Three57m, strongm and Hypetia,

I have written a small test app that has three command buttons, each attempting to your code, respectively. Strongm's is the only one that will compile, so, for now, Three57's and Hypetia's (which appears to rely on Three's) code is partially 'commented out."

strongm --

I tried your code on my development computer and it works fine (as does mine). I will try it at the client location soon and get back to you.

Three57m --

First, you sent me into a NEW WORLD of .RES. Thank you! For the first time, I added a .RES group of files. Got my three media files in under the "WAVE" group like you instructed. My code looks like this:

Code:
'                         module1

'api
Public Declare Function PlaySound Lib "winmm.dll" _
Alias "PlaySoundA" (ByVal lpszName As Any, _
ByVal hModule As Long, ByVal dwFlags As Long) As Long

'constants
Public Const SND_APPLICATION = &H80
Public Const SND_ALIAS = &H10000
Public Const SND_ALIAS_ID = &H110000 
Public Const SND_ASYNC = &H1 
Public Const SND_FILENAME = &H20000 
Public Const SND_LOOP = &H8 
Public Const SND_MEMORY = &H4 
Public Const SND_NODEFAULT = &H2 
Public Const SND_NOSTOP = &H10 
Public Const SND_NOWAIT = &H2000 
Public Const SND_PURGE = &H40 
Public Const SND_RESOURCE = &H40004 
Public Const SND_SYNC = &H0 

'                           form1

Private Function PlayWaveSound() As Boolean

Dim SoundByte() As Byte

On Error GoTo PSError

'Load SoundByte from Resource File
SoundByte = LoadResData(101, "WAVE")

PlaySound SoundByte(0), 0, SND_MEMORY Or SND_NODEFAULT Or SND_ASYNC

PlayWaveSound = True

Exit Function

PSError:
PlayWaveSound = False

End Function

When I run this, I get a "type mismatch" error on compile at "PlaySound SoundByte..." with the word "SoundByte" highlighted, so of course the app doesn't run. Can you detect the error? I tried changing your 'dim SoundByte() as Byte' to 'Static SoundByte() as Byte' like Hypetia recommended, but no luck (I didn't exactly expect that to solve the mismatch problem.) Your help is appreciated and, again, thanks for the journey into .res.

Hypetia --

I tried modifying three57m's code above with your suggestion. I am running this code in a stand alone "SoundTester" app that only has a form. I put the "static..." in the subroutine, then moved it to general declarations (where it would not be allowed), and I really don't have a seperate module attached for this simple test app. Nevertheless, I couldn't get your suggestion to work. Please advise.

THANKS TO ALL OF YOU. I will report back as to whether strongm's simple change made a difference. Three and Hypetia, I would appreciate any additional help you can give.

Ortho

[lookaround] "you cain't fix 'stupid'...
 
Ortho,

You are getting the 'Type mismatch' error because the declaration of PlaySound function does not match what three suggested to you.

[tt]ByRef Sound As Any[/tt] (three57m)

[tt]ByVal lpszName As Any[/tt] (your code)

Argument name doesn't matter, but the argument must be passed by reference in order to pass an array to the API function.

I tried both versions and both played fine.

The main differnce between two versions is that strongm's version plays a file residing on the disk.

On the other hand, three's version does not require the file to be present on the disk, it is embedded in application's executable in the form of a resource.
This is sometimes handy, if the sound to be played is fixed, and small in size. An example is the sounds played in 'The Microsoft Hearts' which are embedded within mshearts.exe.
If you have only three media files, as you suggested, you can once for all embed them in resource and play them directly from within your application.
 
Hypetia,

That did it in my home development environment, THANK YOU. I'll get back to all about whether any of this works in clientworld.

Ortho

[lookaround] "you cain't fix 'stupid'...
 
And, Hypetia, I experienced a problem in that Three's code wouldn't repeat (that is, work again) after it worked once. Used your "Static SoundByte() as Byte and that fixed that!

Clientworld tomorrow.

Ortho

[lookaround] "you cain't fix 'stupid'...
 
Ok I'm in clientworld, and the winner is.....

no one.

All three codes "execute" (i.e., no errors), but no sound (with the volume knob turned up).

Three's version has the media embedded in the app. It doesn't work.

Strongm's small redo of my code doesn't work either.

winmm.dll is present. I tried to download a new copy and delete/replace the .dll, but got error "winmm.dll in use by windows."

Tried to "click" start the media files, and they play just fine through Windows Media Player.

Any ideas?

Thanks,

Ortho

[lookaround] "you cain't fix 'stupid'...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top