Question:
How to play a sound with vb.net?
Sheena W
2008-02-12 00:37:47 UTC
I want to be able to play a sound when a user has sucessfully looged on and a different sound when the log on has failed. Is there and way to do this with vb.net. The log in process is going to happen very quickly and I won't be able to use the Windows media player either. Is there also any way to play a sound without speakers?
Three answers:
searchee #1
2008-02-12 00:50:32 UTC
this is from thescripts.com copyrights are with the developer.



Add the wave file to your project (set 'Build Action' to 'Embedded

Resource'), then

you can use this class to play the sound:



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)'.
Mark F
2008-02-12 00:49:32 UTC
The link below has what you're looking for.



It is possible to play a sound without speakers, but the case speaker can only play tones so it's very complicated. You have to load in the file, extract the raw sound samples, convert them from adaptive pulse-code modulation to pulse-width modulation and then set up a high-priority thread to stream the pulses out to the motherboard speaker.
?
2016-10-07 12:45:15 UTC
hi... First, in case you pay attention music earlier interior an identical DVD Rom perhaps is a configuration issue yet i think of that your issue is extra a hardware extremely than a application issue. i'm sorry to provide help to comprehend that your issue is largely an ordinary cable that is going out of your motherboard on your DVD Rom. in case you pay attention music earlier then examine that your configuration for the audio interior the administration panel are ok. yet like I say... in case you pays attention to music that are reproduction interior the computing device yet no longer on the cd that recommend which you may desire to connect that cable. Is a small cable that is going from the DVD Rom to the motherboard. That cable is almost aways gray with small black terminals. desire this help


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...