Playing *.wav file

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

    #1

    Playing *.wav file

    Hi,

    I'm just trying to play a *.wav file, making use of the unmanaged function
    "sndPlaySou ndA" in winmm.dll, but I still got one question: How can I avoid,
    that the sound file itself has to be seperate file in a specific directory,
    but instead 'integrate' it in my application?

    Thanks
    peter
  • Ken Dopierala Jr.

    #2
    Re: Playing *.wav file

    Hi Peter,

    I created a game programming library for .Net in VB.Net. Here is how I used
    the Windows API to play sound before I switched to FMod from www.fmod.org .
    Below are the support functions:

    #Region " Sound Support"
    Public Enum SND_FLAGS
    SND_SYNC = &H0
    SND_ASYNC = &H1
    SND_NODEFAULT = &H2
    SND_MEMORY = &H4
    SND_LOOP = &H8
    SND_NOSTOP = &H10
    SND_PURGE = &H40
    SND_ALIAS = &H10000
    SND_RESOURCE = &H4004
    SND_NOWAIT = &H2000
    End Enum


    Private Declare Function PlayASound Lib "winmm.dll" Alias "PlaySoundA " _
    (ByVal lpszName As String, ByVal hModule As Integer, ByVal dwFlags As
    Integer) As Integer

    Private Declare Function PlayASound Lib "winmm.dll" Alias "PlaySoundA " _
    (ByVal arrSound() As Byte, ByVal hModule As Integer, ByVal dwFlags As
    Integer) As Integer

    Public Sub kfd_PurgeSound( )
    PlayASound("", 0, SND_FLAGS.SND_P URGE)
    End Sub

    'Plays a .Wav file.

    '---------------------------------------------------------------------------
    -----------------
    'If QuickReturn is TRUE the function returns without waiting for the
    sound to finish playing.
    'If LoopIt is TRUE the function plays the sound over and over again till
    StopSound() is called.
    'If MustPlay is TRUE and another sound is currently playing the function
    returns 0 telling you
    ' that the sound you just sent did not play. If this value is False
    then the sound you send
    ' will end the currently playing sound and then play.
    Public Function kfd_PlaySound(B yVal arrSound() As Byte, Optional ByVal
    QuickReturn As Boolean = True, Optional ByVal LoopIt As Boolean = False,
    Optional ByVal MustPlay As Boolean = False) As Integer
    Dim intFlags, intResult As Integer
    intFlags = SND_FLAGS.SND_N ODEFAULT + SND_FLAGS.SND_M EMORY
    If (QuickReturn = True) Then
    intFlags += SND_FLAGS.SND_A SYNC
    Else
    intFlags += SND_FLAGS.SND_S YNC
    End If
    If (LoopIt = True) Then
    intFlags += SND_FLAGS.SND_L OOP
    End If
    If (MustPlay = True) Then
    intFlags += SND_FLAGS.SND_N OSTOP
    End If
    'Returns 1 if it worked, 0 if it didn't.
    intResult = PlayASound(arrS ound, 0, intFlags)
    End Function

    'Stops the .Wav file that is currently playing.
    Public Function kfd_StopSound() As Integer
    PlayASound(vbNu llString, 0, (SND_FLAGS.SND_ PURGE +
    SND_FLAGS.SND_N ODEFAULT))
    End Function

    Public Function kfd_FillSoundAr ray(ByVal mstrSound As String) As Byte()
    Dim fn As Long
    Dim b() As Byte
    fn = FreeFile()
    FileOpen(fn, mstrSound, OpenMode.Binary )
    ReDim b((LOF(fn) - 1))
    FileGet(fn, b)
    FileClose(fn)
    Return b
    End Function
    #End Region

    Below is the code that plays a sound when a player gets hit using the code
    above.

    Dim sndPlayerHit() As Byte 'A byte array to hold the player getting hit
    sound.
    sndPlayerHit = kfd_FillSoundAr ray(strAppPath + "Sounds\PlayerH it.Wav")
    kfd_PlaySound(s ndPlayerHit)

    The first line is in my declarations section. The second line is run in my
    page load to preload the sound file into an array. The 3rd line is run
    everytime the player is hit with a bullet. Note that the PlayASound API is
    overloaded so you can call that directly with a string representing the path
    if you don't preload your sound into an array, or rewrite your own version
    of kfd_PlaySound to accept a file path. I would recommend moving to FMod
    because it let's you play all your sounds and merges the audio, where as
    with the Windows API you can only play one sound at a time and if you want
    to play another sound the one currently playing gets interrupted. Good
    luck! Ken.

    --
    Ken Dopierala Jr.
    For great ASP.Net web hosting try:
    http://www.webhost4life.com/default.asp?refid=Spinlight
    If you sign up under me and need help, email me.

    "Peter Schmitz" <PeterSchmitz@d iscussions.micr osoft.com> wrote in message
    news:4A9292E0-C705-42EC-8249-C58CC66CD8DE@mi crosoft.com...[color=blue]
    > Hi,
    >
    > I'm just trying to play a *.wav file, making use of the unmanaged function
    > "sndPlaySou ndA" in winmm.dll, but I still got one question: How can I[/color]
    avoid,[color=blue]
    > that the sound file itself has to be seperate file in a specific[/color]
    directory,[color=blue]
    > but instead 'integrate' it in my application?
    >
    > Thanks
    > peter[/color]


    Comment

    Working...