AllAPI Network - The KPD-Team

 
Allapi Network
 API-Guide
 ApiViewer

 API List

 
API Resources
 Tips & Tricks
 VB Tutorials
 Error Lookup
 
Misc Stuff
 VB examples
 VB Tools
 VB Links
 Top Downloads
 
This Site
 Search Engine
 Contact Form
 

Donate to AllAPI.net

How can I play MIDI- and WAVE-files in Visual Basic?

Playing sound effects is actually one of the easiest things to do. To play Wave's, you can use the sndPlaySound-function. "MyFile.wav" is, of course, the sound-file that you want to play. The '1' following the file means that the program should not stop to play the sound. The sound will play and other events can be happening. If you want the whole program to stop while the sound is playing, just change the '1' to a '0'.

For MIDI-files, we use the mciSendString-command. It's not too complicated to understand how it works. "MyFile.mid" is the file that you want to open. You can use any name for the alias.

'In general section
Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long
Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
Private Sub Form_Load()
Const m_MIDI = "MyFile.mid"
Const m_MIDIAlias = "MyFile"
sndPlaySound "MyFile.wav", 1
mciSendString "OPEN " + m_MIDI + " TYPE SEQUENCER ALIAS " + m_MIDIAlias, 0&, 0, 0
mciSendString "PLAY " + m_MIDI + " FROM 0", 0&, 0, 0
mciSendString "CLOSE ANIMATION", 0&, 0, 0
End Sub

To stop the MIDI-file, just use this:

mciSendString "OPEN " + m_MIDI + " TYPE SEQUENCER ALIAS " + m_MIDIAlias, 0&, 0, 0
mciSendString "STOP " + m_MIDI, 0&, 0, 0
mciSendString "CLOSE ANIMATION", 0&, 0, 0

 

 


Copyright © 1998-2007, The Mentalis.org Team - Privacy statement
Did you find a bug on this page? Tell us!
This site is located at http://allapi.mentalis.org/