How to execute *.wav file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • RS

    #1

    How to execute *.wav file

    How can I execute a *.wav file from a VB.NET app
    without opening the windows media player ? I tried
    System.Diagnost ics.Process.Sta rt("C:\rs.wav" )

    but it opened the windows media player.

  • Herfried K. Wagner [MVP]

    #2
    Re: How to execute *.wav file

    Hello,

    "RS" <rls_jls@worldn et.att.net> schrieb:[color=blue]
    >How can I execute a *.wav file from a VB.NET app
    >without opening the windows media player ? I tried
    >System.Diagnos tics.Process.St art("C:\rs.wav" )
    >but it opened the windows media player.[/color]

    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.Reflecti on
    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].GetExecutingAs sembly().GetMan ifestResourceSt ream( _
    Name _
    )
    Dim intLength As Integer = CInt(st.Length)
    Dim abyt(intLength - 1) As Byte
    st.Read(abyt, 0, intLength)
    st.Close()
    m_hgData = Marshal.AllocHG lobal(intLength )
    Marshal.Copy(ab yt, 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(IntPt r.Zero, IntPtr.Zero, SND_PURGE)
    Marshal.FreeHGl obal(m_hgData)
    m_hgData = IntPtr.Zero
    End Sub
    End Class
    ///

    Assuming the name of your project's root namespace is 'MyRootNamespac e', you
    can play the embedded resource "foo.wav" by calling
    'SimpleSound.Pl ay("MyRootNames pace.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.

    --
    Herfried K. Wagner
    MVP ยท VB Classic, VB.NET



    Comment

    Working...