VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
Error Handling (2)
VB6 Database

You are here: Visual Basic > VB6 (Beginners Tutorial)

Tutorial Main Page | Previous Page | Contents | Next Page

Playing Sounds Quickly

• Using the sndPlaySound function in the previous example requires first opening a file, then playing the sound. If you want quick sounds, say in games, the loading procedure could slow you down quite a bit. What would be nice would be to have a sound file ‘saved’ in some format that could be played quickly. We can do that!

• What we will do is open the sound file (say in the Form_Load procedure) and write the file to a string variable. Then, we just use this string variable in place of the file name in the sndPlaySound argument list. We also need to ‘Or’ the SndType argument with the constant SND_MEMORY (this tells sndPlaySound we are playing a sound from memory as opposed to a WAV file). This technique is borrowed from “Black Art of Visual Basic Game Programming,” by Mark Pruett, published by The Waite Group in 1995. Sounds played using this technique must be short sounds (less than 5 seconds) or mysterious results could happen.

Quick Example 8 - Playing Sounds Quickly

We’ll write some code to play a quick ‘bonk’ sound.

  1. Start a new application. Add a command button.

  2. Copy and paste the sndPlaySound Declare statement and the two needed constants (see QuickExample 4). Declare a variable (BongSound) for the sound file. Add SND_MEMORY to the constants declarations. The two added statements are:

    Dim BongSound As String
    Private Const SND_MEMORY = &H4

  3. Add the following general function, StoreSound, that will copy a WAV file into a string variable:

    Private Function StoreSound(ByVal FileName) As String
    ' Load a sound file into a string variable.
    ' Taken from:
    ' Mark Pruett
    ' Black Art of Visual Basic Game Programming
    ' The Waite Group, 1995
    Dim Buffer As String
    Dim F As Integer
    Dim SoundBuffer As String
    On Error GoTo NoiseGet_Error
    Buffer = Space$(1024)
    SoundBuffer = ""
    F = FreeFile
    Open FileName For Binary As F
    Do While Not EOF(F)
    Get #F, , Buffer
    SoundBuffer = SoundBuffer & Buffer
    Close F
    StoreSound = Trim(SoundBuffer)
    Exit Function
    SoundBuffer = ""
    Exit Function
    End Function

  4. Write the following Form_Load procedure:

    Private Sub Form_Load()
    BongSound = StoreSound("bong.wav")
    End Sub

  5. Use this as the Command1_Click procedure:

    Private Sub Command1_Click()
    Call sndPlaySound(BongSound, SND_SYNC Or SND_MEMORY)
    End Sub

  6. Make sure the sound (BONK.WAV) is in the same directory as your application. Run the application. Each time you click the command button, you should hear a bonk!


Tutorial Main Page | Previous Page | Contents | Next Page


Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved