How to use VB program to play WAV files

Source: Internet
Author: User
Program Wednesday, September 2003 1:24 PM when a certain event is completed, the program sends a specific signal to increase the user interface characteristics of the program, such as when the download is complete or a command is selected. The use of a multimedia function, through a VB program to run WAV files is an easy thing. However, the system must install a sound card for this operation.

The API function you need is called PlaySound, and the following is the declaration of the function:

Public Declare Function PlaySound Lib "Winmm.dll" _
Alias "Playsounda" (Byvallpszname as String, _
Byvalhmodule as Long, byvaldwflags as Long) _
As Long

In this function, the first variable is the name, including the path to the running wave file. The second variable is usually not used when running the file, so you can assign it to zero. The last variable contains a tag that controls how the function works.

For current needs, two tags must be required. They are:

Snd_async (value= 1): Run asynchronously, which means that the function is running when the sound is played.
Snd_filename (value = &h20000): The first variable is a filename.
So, the following code plays the sound in the Dingdong.wav file:

PlaySound "Dingdong.wav", CLng (0), _
Snd_async Or Snd_filename

When running audio files in a program, there are two other points to note. First, you have to make sure that a particular audio file exists, or else you will have an error. This process can be accomplished by using one of the following simple functions:

Public Function FileExists (fullfilename) as Boolean

' passed a filename (with path) returns
' True if ' The file exists, False if not.

Dim s

s = Dir (fullfilename)
   
If s = "" Then
FileExists = False
Else
FileExists = True
End If

End Function

Second, the program must have a selection setting that provides the user with a shutdown sound. This process can be achieved by using a global Boolean variable such as gprogramsoundsenabled.

My approach is to handle these needs in a single function, as shown below. (The following code assumes that the audio file is already stored in the program file)

Private Sub playsoundx (filename as String)

' If sound is enabled and filename exists,
' Play the specified sound.

filename = app.path & "\" & filename

If fileexists (filename) and gprogramsoundsenabled Then
PlaySound filename, CLng (0), Snd_async Or snd_filename
End If

End Sub

Using these functions correctly, the program can play any wave file, as follows:

Playsoundx "Dingdong.wav"

Other ways to play audio files from a VB program include multimedia controls on the form. When you don't need these control features, the techniques described above are sufficient and easy to master.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.