Vb. NET in the playback of sound

Source: Internet
Author: User
Tags integer
After the VB6 upgrade to. NET, some people are not clear about the sound processing, such as when the program is wrong, customizing a sound playback, or the program's background music, including game music, and so on. Here are some simple and achievable solutions in vb.net:



1. Beep

The simplest method is to make a sound through the computer's speakers, and the pitch and duration of the sound depends on the hardware and system software, which varies depending on the computer.

Methods such as Beepg and MsgBox are located in Microsoft.VisualBasic.Interaction, and the general default system is automatically loaded. The call is simple, look at the example below.

Dim I as Integer

For I = 1 "to" loop times.

Beep () ' Sound a tone.

Next I



2. Using the Win32 API PlaySound

PlaySound is located in "Winmm.dll" and can play WAV type music depending on the input parameters. The call in vb.net is similar to the VB6:

First declare:

Private Declare Auto Function PlaySound Lib "Winmm.dll" (ByVal lpszsoundname as String, ByVal hmodule as Integer, ByVal DW Flags as Integer) As Integer



Const snd_filename as Integer = &h20000

Const Snd_alias as Integer = &h10000

Const Snd_sync as Integer = &h0

There are many more constants, not one list, the following is the example of playing a WAV file

Dim mstrfilename as String = "C:\eagle2.wav"

PlaySound (mstrfilename, 0, Snd_filename)



Note that the above playback is not synchronized, that is, it will not be finished playing. If you write the above code in a Button.Click event handler, it interrupts the first playback and starts over again when you click the second time.



Of course we can also call the sound of the system:

PlaySound ("Systemstart", 0, Snd_alias Or snd_sync)

' Calls the system startup sound in a synchronized way.



3. Calling the media Player control



Vb. NET does not provide a. NET component of the media player, there is no way we can invoke the previous COM component, Windows Media Player. Of course, in addition to sound, you can play video files.

First add the Media Player control to the toolbar, call or simply write it:



Private Sub playmediafile (ByVal mediafilename as String)

With MediaPlayer1

. Stop ()

. FileName = "C:\mp3\ love does Not Love me. mp3"

. Play ()

End With

End Sub



Summary: Although vb.net than VB6 changed a lot, including real oop, multithreading and so on, but there is still a lot of contact with VB, suggesting that you encounter difficult to solve the problem, try the previous treatment methods, generally can be effective

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.