473,698 Members | 2,601 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sndPlaySound versus PlaySound

Hi,

I have an application that has several forms. Each form acts as a
monitor of a gateway system. If the gateway appears to be dead, the
application should play a .wav file (not resource) until the user
clicks a button to acknowledge the situation. To keep things simple,
lets assume there is only one monitor.

If I use the old sndPlaySound function, with the code:

SND_ASYNC = &H1
SND_LOOP = &H8

'Gateway Failed - Sound the alarm.
sndPlaySound(st rFileName, SND_ASYNC OR SND_LOOP)
MessageBox.Show ("Check Gateway!")
MessageBox.Show ("Resume Monitor?")
sndPlaySound(No thing, &H2)

the wav file is played in a loop until the user the user responds to
the second MessageBox.

However, if I use the preferred (because its newer) PlaySound function
with the code:

PlaySound(strFi leName, Nothing, SND_ASYNC OR SND_LOOP)
MessageBox.Show ("Check Gateway!")
MessageBox.Show ("Resume Monitor?")
PlaySound(Nothi ng, Nothing, &H2)

The wav file is played just once and the first message box is
displayed.

Can anyone tell me how I should fiddle with the PlaySound parameters to
get this working?

My guess is that the sound produced when the message box is displayed
is clearing the the wav file when the PlaySound function is used. For
some reason, this does not happen with the sndPlaySound function.

If it helps, my next question will be how do I play two alarms at the
same time i.e. if two gateways fail simultaneously how do I prevent the
second alarm from purging the first alarm?

Thanks in advance,

Andy

Nov 21 '05 #1
9 11839
"Andy" <an************ *********@gmail .com> schrieb:
However, if I use the preferred (because its newer) PlaySound function
with the code:

PlaySound(strFi leName, Nothing, SND_ASYNC OR SND_LOOP)


Post your declaration of 'PlaySound'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #2
Here are the two API function declarations:

Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA "
(ByVal strSoundFileNam e As String, _
ByVal hModule As Long, ByVal lngOptions As Long) As
Long
'
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySou ndA"
(ByVal lpszSoundFile As String, _
ByVal uFlags As Long) As Long
Please note that I'm using Option Explicit and Option Strict.

Thanks,

Andy

Nov 21 '05 #3
"Andy" <an************ *********@gmail .com> schrieb:
Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA "
(ByVal strSoundFileNam e As String, _
ByVal hModule As Long, ByVal lngOptions As Long) As
Long
'
Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySou ndA"
(ByVal lpszSoundFile As String, _
ByVal uFlags As Long) As Long


Your declarations are invalid for VB.NET. Note that 'Long' is a 64-bit
datatype in .NET but the function expects 32-bit values. Use these
declarations instead:

\\\
Private Declare Auto Function PlaySound Lib "winmm.dll" ( _
ByVal pszSound As String, _
ByVal hmod As IntPtr, _
ByVal fdwSound As Int32 _
) As Boolean

Private Declare Auto Function sndPlaySound Lib "winmm.dll" ( _
ByVal lpszSound As String, _
ByVal fuSound As Int32, _
) As Boolean
///

When calling 'PlaySound', pass 'IntPtr.Zero' in its 'hmod' parameter unless
you want to play a sound which is embedded as Win32 resource.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4
Hi Andy,

Actually I wonder why your sndPlaySound declaration works. 8=]

Anyway, try this ones (modified and a bit improved):

~
Friend Declare Auto Function sndPlaySound Lib "winmm.dll" ( _
<MarshalAs(Unma nagedType.LPTSt r), [In]()> ByVal lpszSound As
String, _
ByVal fuSound As sndPlaySoundFla g _
) As Boolean

Friend Declare Auto Function PlaySound Lib "winmm.dll" ( _
<MarshalAs(Unma nagedType.LPTSt r), [In]()> ByVal pszSound As
String, _
ByVal hmod As IntPtr, _
ByVal fdwSound As PlaySoundFlag _
) As Boolean

<Flags()> Friend Enum sndPlaySoundFla g
Async = &H1
[Loop] = &H8
Memory = &H4
NoDefault = &H2
NoStop = &H10
Sync = &H0
End Enum

<Flags()> Friend Enum PlaySoundFlag
Application = &H80
[Alias] = &H10000
AliasID = &H110000
Async = &H1
FileName = &H20000
[Loop] = &H8
Memory = &H4
NoDefault = &H2
NoStop = &H10
NoWait = &H2000
Purge = &H40
Resource = &H40004
Sync = &H0
End Enum
~
Please note that I'm using Option Explicit and Option Strict.

I wish Option Explicit and Option Strict could help in this area 8=]

Hope this helps,
Roman


Nov 21 '05 #5
Hi Herfried and Dragon,

Many thanks to you both for your suggestions.

Dragon: Your declarations produced warning messages for MarshalAs and
[In] e.g. "Type MarshalAs is not defined" so I used Herfried's
declaration without the trailing coma following ByVal fuSound As Int32
;) However, I did use your PlaySoundFlag enumeration :)

The use of PlaySound is now working i.e. looping.

As predicted I am now at the point where I want to have multiple
monitors. If two monitors raise an alarm, the second alarm sound
replaces that of the first. When I acknowledge the alarm of the second
the sound is (correctly) stopped and there is silence i.e. the alarm
sound of the first monitor was not 'hidden' but (I guess) purged by the
second alarm being generated.

Is it possible to have two (or more) alarm sounds played at the same
time. If so, is it difficult?

Thanks again,

Andy

Nov 21 '05 #6
"Andy" <an************ *********@gmail .com> schrieb:
Dragon: Your declarations produced warning messages for MarshalAs and
[In] e.g. "Type MarshalAs is not defined"


The 'System.Runtime .InteropService s' namespace must be imported in order to
use these classes.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #7
Hi,

As Herfied states, MarshalAsAttrib ute and InAttribute are located in
System.Runtime. InteropServices namespace.
It's always wise to import it when you're dealing with interop.
Obviously, you can write full-qualified name like
System.Runtime. InteropServices .In().

As for your second question...

I don't think it's possible to play 2 sounds at the same time with
WinMM. Nevertheless, I'm not an expert at it, so I may mistake.
You can use some workaround here, for example, insert a global flag
indicating the number of alarms and shut down sound only if it's 0.
Quick & dirty example:

~
nRedAlerts += CByte(1)
PlaySound("!!!. wav", IntPtr.Zero, PlaySoundFlag.A sync Or
PlaySoundFlag.L oop Or PlaySoundFlag.F ileName)
MessageBox.Show ("!!!")
nRedAlerts -= CByte(1)
If nRedAlerts = 0 Then PlaySound(Nothi ng, IntPtr.Zero,
PlaySoundFlag.N oDefault)
~

Roman
Nov 21 '05 #8
Hi Roman,

Many thanks for the suggestion.

I have followed your advice and used global to track the number of
alerts and the associated wav files.

Cheers,

Andy

Nov 21 '05 #9
Your example declarations and enums work great in a regular vb .net app. I
will hang on to them for later use.

Dov you know what declarations I can use for a mobile device app? PocketPC
2002

Thanks,

Jerod
"Dragon" wrote:
Hi Andy,

Actually I wonder why your sndPlaySound declaration works. 8=]

Anyway, try this ones (modified and a bit improved):

~
Friend Declare Auto Function sndPlaySound Lib "winmm.dll" ( _
<MarshalAs(Unma nagedType.LPTSt r), [In]()> ByVal lpszSound As
String, _
ByVal fuSound As sndPlaySoundFla g _
) As Boolean

Friend Declare Auto Function PlaySound Lib "winmm.dll" ( _
<MarshalAs(Unma nagedType.LPTSt r), [In]()> ByVal pszSound As
String, _
ByVal hmod As IntPtr, _
ByVal fdwSound As PlaySoundFlag _
) As Boolean

<Flags()> Friend Enum sndPlaySoundFla g
Async = &H1
[Loop] = &H8
Memory = &H4
NoDefault = &H2
NoStop = &H10
Sync = &H0
End Enum

<Flags()> Friend Enum PlaySoundFlag
Application = &H80
[Alias] = &H10000
AliasID = &H110000
Async = &H1
FileName = &H20000
[Loop] = &H8
Memory = &H4
NoDefault = &H2
NoStop = &H10
NoWait = &H2000
Purge = &H40
Resource = &H40004
Sync = &H0
End Enum
~
Please note that I'm using Option Explicit and Option Strict.

I wish Option Explicit and Option Strict could help in this area 8=]

Hope this helps,
Roman


Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
15429
by: bballmitch | last post by:
Why won't the following code work? char soundfile2 = "C:/Windows/Media/pinky and the brain.wav"; PlaySound(soundfile2,NULL,SND_FILENAME|SND_SYNC|SND_ASYNC); getchar(); i put libwinmm.a in the parameters and i have all the right headerfiles in there, but it won't work. it says that i must declare SND_FILENAME ,
3
4640
by: Harry J. Smith | last post by:
I added some sounds to my application, but the example in the msdn Library did not work. It had: public static extern bool PlaySound( string szSound, IntPtr hMod, PlaySoundFlags flags ); The file coredll was not found. I changed it to and it now works. What is the best way to handle this? -Harry
4
4509
by: rcattral | last post by:
Has anybody been using the PlaySound() function to play WAV files from within a DLL? Here is what I normally do. Since upgrading to 7.1 NET after using VC 6.0 for quite some time, I found that I had to add the following lines in order to use the multimedia extensions. In stdafx.h #include <MMSystem.h>
2
1684
by: Joe Thompson | last post by:
Hi, I am trying to use PlaySound in a VC++.net Windows app (VS 2003). I can use it to play a file but now I want to play it from a resource. I have two questions: How do I add a wav file to my project as a resource (steps please) How do I call PlaySound once that I have the wav file as part of the resource.
19
2107
by: Nak | last post by:
Hi there, *fingers crossed that the newsserver works long enough for me to see any replies to this!!* I'm having crashes when using SndPlaySound with SND_ASYNC and SND_MEMORY in VB.NET. The sounds are being loaded from embedded resources and stored into byte arrays, which are then being stored in a hashtable for easy retrieval second time around. Occasionally a get a funny "beep" sound and the application crashes, the report tells...
10
2476
by: Ot | last post by:
I found information on PlaySound. I implemented it in my program and it works just dandy. The only little problem is that I have to package the ..wav files and send them along since PlaySound plays a sound file. One of the options I found in a nice description of PlaySound says that there are a couple of other choices. 1) The sound can be in the registry and actually controlled/selected by the end user.
2
2549
by: Ansh | last post by:
i want to play a .wav file using sndPlaySound and PlaySound function but both function doesn't works in the expected way... i just hear a click sound after execution of these functions.... not the original recorded sound of .wav file.... on the other hand when i make use of wave functions to play sound it works fine... can anybody tell that what exactly the problem is??? all docs i read suggests that these functions works fine if we hav...
3
10636
by: Jared | last post by:
I'm using the first code sample below to play WAV files stored as embedded resources. For some reason I *occasionally* get scratching and crackling. I'm using a couple WAVs that ship with Windows, and they play fine outside of my app. This code works, but has the random "scratching" problem: private static void PlayResource(string audioRes) { // Get the resource into a stream
0
1898
by: poppy | last post by:
I tried to play a wav file with function Playsound() but it played only the first seconds of the song.I would like to play whole the song.What might de be wrong?Here is my code in case someone can help. /* This uses the high level WAVE API PlaySound() to play a WAVE file in memory. I simply open the WAVE file called "nagelas.wav" (ie, in order for this example to work, you should make sure that you have such a WAVE file on your...
0
8683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9170
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8876
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7741
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6531
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5867
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2341
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.