Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to play sound ?

0 views
Skip to first unread message

MingChih Tsai

unread,
Aug 11, 2003, 6:00:03 AM8/11/03
to
What is the simplest way to play sounds in the windows form ?

Thanks !!

--
QQ


Nak

unread,
Aug 11, 2003, 6:09:39 AM8/11/03
to
Hiya MingChih,

No .NET support, the only way without using directX or some 3rd party
classes would be to use the API

http://www.mentalis.org/apilist/sndPlaySound.shtml

Nick.

"MingChih Tsai" <m...@seed.net.tw> wrote in message
news:%234dnO9%23XDH...@TK2MSFTNGP09.phx.gbl...

spam xyz@null.com Vincent Wiegel

unread,
Aug 11, 2003, 6:37:56 AM8/11/03
to
' Flags used by PlaySound
Private Const SND_FILENAME As Long = &H20000L ' name is file name
Private Const SND_MEMORY As Long = &H4L ' pszSound points to a
memory file
Private Const SND_PURGE As Long = &H40L ' purge non-static events
for task
Private Const SND_NODEFAULT As Long = &H2L ' silence (!default) if
sound not found
Private Const SND_ALIAS As Long = &H10000L ' name is a registry
alias
Private Const SND_ASYNC As Long = &H1L ' play asynchronously
Private Const SND_RESOURCE As Long = &H40004L ' name is resource name
or atom


Private Declare Auto Function PlaySound Lib "winmm.dll" (ByVal soundName
As String, ByVal hModule As IntPtr, ByVal dwFlags As Long) As Long

---some procedure
Dim defaultSound As String = "sound.wav"
PlaySound(defaultSound, IntPtr.Zero, SND_FILENAME + SND_ASYNC)
---


~V


"MingChih Tsai" <m...@seed.net.tw> wrote in message
news:%234dnO9%23XDH...@TK2MSFTNGP09.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Aug 11, 2003, 6:54:47 AM8/11/03
to
Hello,

"MingChih Tsai" <m...@seed.net.tw> schrieb:


> What is the simplest way to play sounds in the windows form ?

Add the wave file to your project (build action = embedded resource),
then
you can use this class to play the sound:

\\\
'
' Copyright (C) 2003 by Herfried K. Wagner.
'
Imports System.IO
Imports System.Reflection
Imports System.Runtime.InteropServices

Public Class SimpleSound
Private Declare Auto Function PlaySound Lib "winmm.dll" ( _
ByVal pszSound As IntPtr, _
ByVal hModule As IntPtr, _
ByVal dwFlags As Int32 _
) As Boolean

Private Const SND_ASYNC As Int32 = &H1
Private Const SND_MEMORY As Int32 = &H4
Private Const SND_LOOP As Int32 = &H8
Private Const SND_PURGE As Int32 = &H40

Private Shared m_hgData As IntPtr

Public Shared Sub Play(ByVal Name As String, Byval [Loop] As Boolean)
If Not m_hgData.Equals(IntPtr.Zero) Then
StopPlaying()
End If
Dim st As Stream = _
[Assembly].GetExecutingAssembly().GetManifestResourceStream( _
Name _
)
Dim intLength As Integer = CInt(st.Length)
Dim abyt(intLength - 1) As Byte
st.Read(abyt, 0, intLength)
st.Close()
m_hgData = Marshal.AllocHGlobal(intLength)
Marshal.Copy(abyt, 0, m_hgData, intLength)
Dim Flags As Int32 = SND_MEMORY Or SND_ASYNC
If [Loop] Then
Flags = Flags Or SND_LOOP
End If
PlaySound( _
m_hgData, _
IntPtr.Zero, _
Flags _
)
End Sub

Public Shared Sub [Stop]()
If Not m_hgData.Equals(IntPtr.Zero) Then
StopPlaying()
End If
End Sub

Private Shared Sub StopPlaying()
PlaySound(IntPtr.Zero, IntPtr.Zero, SND_PURGE)
Marshal.FreeHGlobal(m_hgData)
m_hgData = IntPtr.Zero
End Sub
End Class
///

Assuming the name of your project's root namespace is "MyRootNamespace",
you
can play the embedded resource foo.wav by calling
"SimpleSound.Play("MyRootNamespace.foo.wav", True)".

HTH,
Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet


Alexandre Moura

unread,
Aug 15, 2003, 5:02:47 PM8/15/03
to
The absolute simplest way, if you just want an audible attention call -
assuming your machine still supports it:

Module Module1

Sub Main()
Beep()
End Sub

End Module

Note that a lot of modern machine come without the pc speaker - I don't
think beep works without it.

Alex

--------------------
>From: "MingChih Tsai" <m...@seed.net.tw>
>Subject: How to play sound ?
>Date: Mon, 11 Aug 2003 18:00:03 +0800
>Lines: 8
>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
>Message-ID: <#4dnO9#XDHA...@TK2MSFTNGP09.phx.gbl>
>Newsgroups: microsoft.public.dotnet.languages.vb
>NNTP-Posting-Host: ll-218-32-201-39.ll.sparqnet.net 218.32.201.39
>Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:126960
>X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Nak

unread,
Aug 15, 2003, 5:51:31 PM8/15/03
to
I was thinking of changing my pc speaker for a 60" sub woofer, with slight
modification to my case it should prove excellent! What do you think?

Nick.

"Alexandre Moura" <amo...@online.microsoft.com> wrote in message
news:13LZgC3Y...@cpmsftngxa06.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Aug 15, 2003, 7:08:02 PM8/15/03
to
Hello,

"Nak" <a...@a.com> schrieb:


> I was thinking of changing my pc speaker for a 60" sub woofer, with
slight
> modification to my case it should prove excellent! What do you think?

You can change the sound played when calling beep in the control panel.

Huh...

Regards,


Herfried K. Wagner
--
MVP · VB Classic, VB .NET
http://www.mvps.org/dotnet

Purpose: Help Protect Your Computer Today (Immediately)
Action: Read about the Blaster worm and update your software
immediately. Check the Security site for more information and steps you
should take to help protect your systems.
URL: http://www.microsoft.com/security/incident/blast.asp


Nak

unread,
Aug 16, 2003, 4:49:47 AM8/16/03
to
Yeah, Ill turn that into some heavy drum and bass fill, what do you think?

;-)

Nick.

"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message
news:ebvcWI4Y...@TK2MSFTNGP09.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Aug 16, 2003, 6:38:17 AM8/16/03
to
Hello,

"Nak" <a...@a.com> schrieb:


> Yeah, Ill turn that into some heavy drum and bass fill, what do you
think?

NP: "The Microsoft Sound.wav"

;-)))

Nak

unread,
Aug 16, 2003, 6:40:36 AM8/16/03
to
Haha :-)

"Herfried K. Wagner [MVP]" <hirf...@m.activevb.de> wrote in message

news:%23U3FDK%23YDH...@TK2MSFTNGP10.phx.gbl...

Alexandre Moura

unread,
Aug 27, 2003, 4:24:55 PM8/27/03
to
Sounds cool, but you really should get some hidraulics to get you case
jumping - and a rear spoiler, and I guess some alloy fans... some neon
lights underneath might also look good ;)

--------------------
>From: "Nak" <a...@a.com>
>References: <#4dnO9#XDHA...@TK2MSFTNGP09.phx.gbl>
<13LZgC3Y...@cpmsftngxa06.phx.gbl>
<ufkHld3Y...@TK2MSFTNGP12.phx.gbl>
<ebvcWI4Y...@TK2MSFTNGP09.phx.gbl>
<ugI5YN9Y...@TK2MSFTNGP12.phx.gbl>
<#U3FDK#YDHA...@TK2MSFTNGP10.phx.gbl>
>Subject: Re: How to play sound ?
>Date: Sat, 16 Aug 2003 11:40:36 +0100
>Lines: 29


>X-Priority: 3
>X-MSMail-Priority: Normal
>X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
>X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165

>Message-ID: <OOy6TL#YDHA...@TK2MSFTNGP09.phx.gbl>
>Newsgroups: microsoft.public.dotnet.languages.vb
>NNTP-Posting-Host: dsl213-218-225-233.as15444.net 213.218.225.233
>Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
>Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:128282
>X-Tomcat-NG: microsoft.public.dotnet.languages.vb

Nak

unread,
Aug 27, 2003, 4:38:41 PM8/27/03
to
> Sounds cool, but you really should get some hidraulics to get you case
> jumping - and a rear spoiler, and I guess some alloy fans... some neon
> lights underneath might also look good ;)

Haha :-) Then I could be the computer equivilent of a "boy racer"!

Do you think some fluffy dice on my monitor also?

Nick.


Alexandre Moura

unread,
Sep 18, 2003, 3:26:36 PM9/18/03
to
I don't know - I was always a bit partial to the tree freshener...

William Ryan

unread,
Sep 18, 2003, 3:34:24 PM9/18/03
to
Look on Google for PlaySound C#

"Alexandre Moura" <amo...@online.microsoft.com> wrote in message
news:O95%23LrhfD...@cpmsftngxa06.phx.gbl...

Herfried K. Wagner [MVP]

unread,
Sep 18, 2003, 5:16:22 PM9/18/03
to
Hello,

"Alexandre Moura" <amo...@online.microsoft.com> schrieb:


> I don't know - I was always a bit partial to the tree freshener...

???

Add the wave file to your project (set 'Build Action' to 'Embedded
Resource'), then

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
http://www.mvps.org/dotnet


Alexandre Moura

unread,
Sep 19, 2003, 4:02:56 PM9/19/03
to
Sorry guys - I was replying to an older thread that has deviated quite a
lot from the original title -thanks anyway

0 new messages