Playing in VB.NET
BY MONTAQUE
After the VB6 is upgraded to .NET, some people don't know the process of sound, such as the program, customize a sound play, or the background music of the program, including game music, etc. Here is a program that is simpler and achievable in VB.NET:
1. BEEP
The simplest way, emitting through the computer's speakers, the sound and duration of the sound depends on the hardware and system software, which varies depending on the computer.
BEEPG is in Microsoft.VisualBasic.Intection, and the default system is automatically loaded in Microsoft.visualBasic.Ist. The call is very simple, look at the example below.
DIM I as integer
FOR i = 1 to 100 'loop 100 Times.
Beep () 'Sound a Tone.
Next i
2. Win32 API Plays
Plays is in "Winmm.dll", you can play the WAV type music according to the different input parameters. Calling with VB6 in VB.NET is similar:
Declaration first:
Private Declare Auto Function Plays LIB "Winmm.dll" (Byval HModule As Integer, Byval DWFLAGS AS INTEGER) AS INTEGER
Const SND_FILENAME AS INTEGER = & H20000
Const Snd_Alias As INTEGER = & H10000
Const SND_SYNC AS INTEGER = & H0
There are still many constants, not one list, the following is an example of playing a WAV file.
DIM mstrfilename as string = "c: /eagle2.wav"
Plays (MstrFileName, 0, SND_FILENAME)
Note that the playback is not synchronized, that is, it does not play it over. If you write the above code in an event handler in a Button.Click, it will interrupt the first play and restart.
Of course, we can also call the system's voice:
Playsound ("SystemStart", 0, SND_ALIAS OR SND_SYNC)
'Call the sound at the system startup in synchronization.
3. Call media player control
VB.NET does not provide a .NET component of the media player, no way we still call previous COM components Windows Media Player. Of course, in addition to the sound, you can also play video files.
First add the media player control to the toolbar, call or simply write :)
Private sub planmediafile (byval MediaFileName As String)
With MediaPlayer1
.Stop ()
.Filename = "c: / mp3 / love doesn't love me. MP3"
.Play ()
End with
End Sub
Summary: Although VB.NET has changed a lot, including true OOP, multi-thread, etc., but always have a lot of contact with VB. It is recommended that when you encounter problems that are difficult to solve, try the previous processing method, general Can play J