VB Mania

Text Box: Previous Example
Text Box: Next Example
Text Box: VB-Mania Core

Play a Wave File

Information:

In this example, you will be able to play wave files using sndPlaySound function of winmm library.

Illustration:

 

What You Need?:

1. One Form

2. One CommandButton name cmdPlay

Private Declare Function sndPlaySound Lib "winmm" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long

 

Private Sub PlaySound(strFileName As String)

    sndPlaySound strFileName, 1

End Sub

 

Private Sub cmdPlay_Click()

PlaySound "c:\windows\media\chimes.wav"

End Sub

Code:

 

Explanation:

CommandButton cmdPlay will evoke the playsound private function. In playsound function, we used the member of winmm library, sndPlaySound, to play a wave file dictated in the parameter strFileName. To play a file, you should write its full path.

Hint:

· Sndplaysound will only play wave files.

· To play a wave file, named chimes.wav for example, in the program directory. Use this: PlaySound App.Path & "\chimes.wav" Where as App.path will fill in the program directory full path.

· To play MIDI files, see Play an MIDI File under using mciSendString section.

VB-Pedia:

· sndPlaySound: is function of winmm library that plays a waveform sound specified either by a filename.

Parameters:

¨ lpszSoundName

A string that specifies the sound to play. This parameter can be either an entry in the registry or in WIN.INI that identifies a system sound, or it can be the name of a waveform-audio file. (If the function does not find the entry, the parameter is treated as a filename.) If this parameter is NULL, any currently playing sound is stopped.

¨ uFlags

Flags for playing the sound.

Sponsored Links

Google
 
Web vbmania2006.brinkster.net
planet-source-code.com support.microsoft.com