Hello,
RS said:
How can I execute a *.wav file from a VB.NET app
without opening the windows media player ? I tried
System.Diagnostics.Process.Start("C:\rs.wav")
but it opened the windows media player.
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)'.
If you want to have the Wave file in a separate file, have a look at the
documentation for the 'PlaySound' API. You will have to change its
parameters.