Shoutbox

playing a .wav file in VB - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: playing a .wav file in VB (/showthread.php?tid=27398)

playing a .wav file in VB by .blade// on 06-18-2004 at 10:10 PM

I can't remember anymore how to, but I know it's possible to play a .wav file in VB WITHOUT using the Windows Media component, anyone have a little code.


Also: anyone know a quick code to save a file name in a text document without using the Rich Text Document component?


RE: playing a .wav file in VB by Hah on 06-18-2004 at 10:15 PM

http://www.vbcodemagician.dk/tips/media_wavplay.htm

thats a very simple example of how to play a wav file in vb.

As for Also how to save a filename in a text document without rich edit control:

Have you tried:

code:
filechan = freefile
filename = "hello.txt"
Open filename for append as fileChan
    Print #filechan, "The text to be appended to end of file."
Close #filechan


Hope i understood what you meant there. If you could explain in more detail I'll definately be able to help you.

Hah
RE: playing a .wav file in VB by .blade// on 06-18-2004 at 10:21 PM

quote:
Originally posted by Hah
http://www.vbcodemagician.dk/tips/media_wavplay.htm

thats a very simple example of how to play a wav file in vb.

As for Also how to save a filename in a text document without rich edit control:

Have you tried:

code:
filechan = freefile
filename = "hello.txt"
Open filename for append as fileChan
    Print #filechan, "The text to be appended to end of file."
Close #filechan


Hope i understood what you meant there. If you could explain in more detail I'll definately be able to help you.

Hah

thanks, that's exactly what I needed, but I forgot to ask how to open that file again with out the RTD :P

edit: NVM, I am not going to be needing the open/close code anyways...
RE: playing a .wav file in VB by .blade// on 06-18-2004 at 10:59 PM

Sorry for the double post, but is there a way to play the file without freezing the form?


RE: playing a .wav file in VB by Choli on 06-18-2004 at 11:23 PM

quote:
Originally posted by Hah
http://www.vbcodemagician.dk/tips/media_wavplay.htm

thats a very simple example of how to play a wav file in vb.
That example uses the sndPlaySound API, that is obsolete. You should use the PlaySound one instead (that also lets you play a sound in background; ie. without freezing the form):

PlaySound function:
code:
Declare Function PlaySound Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Alternate Declare for when using a resource or memory location:
code:
Declare Function PlaySound_Res Lib "winmm.dll" Alias "PlaySoundA" (ByVal lpszName As Long, ByVal hModule As Long, ByVal dwFlags As Long) As Long
I personally prefer thise second declaration. If you can use the StrPtr function you should use this second one, imo

Platforms: Win 32s, Win 95/98, Win NT

PlaySound plays a waveform sound through the speakers. This sound could be a .wav file, a system event sound (such as the system startup sound), or a sound resource stored in an application. Note that when the function needs to play an application resource or a RAM-loaded sound, Visual Basic users must use the alternate declare of the function in order to pass the numeric identifier of the sound instead of a string. The function returns 0 if an error occured, or a non-zero value if successful.

  • lpszName
    The name or some other identifier of the sound. Its exact format depends on the flags passed as dwFlags.

  • hModule
    A handle to the application module containing the sound resource the play, if needed. If the function does not need this information, pass 0 for this parameter.

  • dwFlags
    Zero or more of the following flags specifying what lpszName refers to and how to play the sound:
    • SND_ALIAS = &H10000
      lpszName is a string identifying the name of the system event sound to play.
    • SND_ALIAS_ID = &H110000
      lpszName is a string identifying the name of the predefined sound identifier to play.
    • SND_APPLICATION = &H80
      lpszName is a string identifying the application-specific event association sound to play.
    • SND_ASYNC = &H1
      Play the sound asynchronously -- return immediately after beginning to play the sound and have it play in the background.
    • SND_FILENAME = &H20000
      lpszName is a string identifying the filename of the .wav file to play.
    • SND_LOOP = &H8
      Continue looping the sound until this function is called again ordering the looped playback to stop. SND_ASYNC must also be specified.
    • SND_MEMORY = &H4
      lpszName is a numeric pointer refering to the memory address of the image of the waveform sound loaded into RAM.
    • SND_NODEFAULT = &H2
      If the specified sound cannot be found, terminate the function with failure instead of playing the SystemDefault sound. If this flag is not specified, the SystemDefault sound will play if the specified sound cannot be located and the function will return with success.
    • SND_NOSTOP = &H10
      If a sound is already playing, do not prematurely stop that sound from playing and instead return with failure. If this flag is not specified, the playing sound will be terminated and the sound specified by the function will play instead.
    • SND_NOWAIT = &H2000
      If a sound is already playing, do not wait for the currently playing sound to stop and instead return with failure.
    • SND_PURGE = &H40
      Stop playback of any waveform sound. lpszName must be an empty string.
    • SND_RESOURCE = &H4004
      [ì]lpszName[/i] is the numeric resource identifier of the sound stored in an application. hModule must be specified as that application's module handle.
    • SND_SYNC = &H0
      Play the sound synchronously -- do not return until the sound has finished playing.

Example:

code:
' First play the SystemStart event sound synchronously.  Then loop
' playing the file C:\Sounds\scream.wav for 5 seconds before stopping.
Dim retval As Long  ' return value of the function

' Synchronously play the SystemStart sound.  This function returns when the sound is done.
retval = PlaySound("SystemStart", 0, SND_ALIAS Or SND_SYNC)

' Now loop the .wav file for five seconds before purging its playback.  Note that
' we don't want the default sound to play if the file is not found.
retval = PlaySound("C:\Sounds\scream.wav", 0, SND_FILENAME Or SND_ASYNC Or SND_NODEFAULT Or SND_LOOP)
Sleep 5000  ' wait for 5 seconds while sound loops
retval = PlaySound("", 0, SND_PURGE Or SND_NODEFAULT)  ' stop playback